New Text Document
New Text Document
// Shared variable
int share = 1;
// Thread functions
void *fun1() {
int x;
x = share;
printf("Thread1: %d\n", x);
x++;
printf("Local Thread1: %d\n", x);
sleep(1); // Sleep for 1 second
share = x;
printf("Value of shared process (Thread1): %d\n", share);
return NULL;
}
void *fun2() {
int y;
y = share;
printf("Thread2: %d\n", y);
y--;
printf("Local Thread2: %d\n", y);
sleep(1); // Sleep for 1 second
share = y;
printf("Value of shared process (Thread2): %d\n", share);
return NULL;
}
int main() {
pthread_t thread1, thread2; // Thread identifiers
// Create threads
pthread_create(&thread1, NULL, fun1, NULL);
pthread_create(&thread2, NULL, fun2, NULL);
return 0;
}
int main() {
pthread_t a_thread; //Declare Thread
pthread_create(&a_thread, NULL, thread_function,NULL);
pthread_join(a_thread, NULL); //it waits
int main() {
pid_t p;
p = fork();
if(p==0) //child
{
printf("I am child %d \n", getpid());
printf("My parent if %d \n", getpid());
}
else {
printf("I am the PID %d \n", getpid());
printf("PID %d \n",p);
}
int main() {
pthread_t t; // Thread identifier
struct arg_struct args; // Argument structure
return 0;
}