Code written in C, use gcc -pthread to compile if you are in Linux /* Processes vs.
Question:
Code written in C, use gcc -pthread to compile if you are in Linux
/* Processes vs. threads storage example */
#include
#include
#include
#include
/*This data is shared by the thread(s) */
int this_is_global;
/*This is the thread function */
void thread_func(void *ptr);
int main() {
int local_main;
int pid, status;
pthread_t thread1, thread2;
printf("First, we create two threads to see better what context they share...");
this_is_global = 1000;
printf("Set this_is_global to: %d", this_is_global);
/* create the two threads and wait for them to finish */
pthread_create(&thread1, NULL, (void*)&thread_func, (void*) NULL);
pthread_create(&thread2, NULL, (void*)&thread_func, (void*) NULL);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
printf("After threads, this_is_global = %d", this_is_global);
printf("Now that the threads are done, let's call fork..");
/* set both local and global to equal value */
local_main = 17;
this_is_global = 17;
printf("Before fork(), local_main = %d, this_is_global = %d",
local_main, this_is_global);
/* create a child process. Note that it inherits everything from the parent */
pid=fork();
if (pid == 0) { /* this is the child */
printf("Child : pid: %d, local address: %#X, global address: %#X",
getpid(), &local_main, &this_is_global);
/* change the values of both local and global variables */
local_main = 13;
this_is_global = 23;
printf("Child : pid: %d, set local_main to: %d; this_is_global to: %d",
getpid(), local_main, this_is_global);
exit(0);
}
else { /* this is the parent */
printf("Parent: pid: %d, lobal address: %#X, global address: %#X",
getpid(), &local_main, &this_is_global);
wait(&status);
/* print the values of both variables after the child process is finished */
printf("Parent: pid: %d, local_main = %d, this_is_global = %d",
getpid(), local_main, this_is_global);
}
exit(0);
}
void thread_func(void *dummy) {
int local_thread;
printf("Thread: %lu, pid: %d, addresses: local: %#X, global: %#X",
pthread_self(), getpid(), &local_thread, &this_is_global);
/* increment the global variable */
this_is_global++;
printf("Thread: %lu, incremented this_is_global to: %d",
pthread_self(), this_is_global);
pthread_exit(0);
}
8) Run the above program and observe its output. Following is a sample output:
First, we create two threads to see better what context they share...
Set this_is_global to: 1000
Thread: 3070053232, pid: 10487, addresses: local: 0XB6FD438C, global: 0X804A040
Thread: 3070053232, incremented this_is_global to: 1001
Thread: 3078445936, pid: 10487, addresses: local: 0XB77D538C, global: 0X804A040
Thread: 3078445936, incremented this_is_global to: 1002
After threads, this_is_global = 1002
Now that the threads are done, let's call fork..
Before fork(), local_main = 17, this_is_global = 17
Parent: pid: 10487, lobal address: 0XBFF5CB0C, global address: 0X804A040
Child : pid: 10490, local address: 0XBFF5CB0C, global address: 0X804A040
Child : pid: 10490, set local_main to: 13; this_is_global to: 23
Parent: pid: 10487, local_main = 17, this_is_global = 17
1- Did this_is_global change after the threads have finished? Why?
2- Are the local addresses the same in each thread? What about the global addresses?
3- Did local_main and this_is_global change after the child process has finished? Why?
4- Are the local addresses the same in each process? What about global addresses? What happened?