0% found this document useful (0 votes)
23 views3 pages

New Text Document

The document contains multiple C code snippets demonstrating the use of threads and processes. It includes examples of creating threads with pthreads, using shared variables, and forking processes. Each snippet illustrates different functionalities such as thread synchronization, argument passing to threads, and process creation.

Uploaded by

Habib Ur Rehman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views3 pages

New Text Document

The document contains multiple C code snippets demonstrating the use of threads and processes. It includes examples of creating threads with pthreads, using shared variables, and forking processes. Each snippet illustrates different functionalities such as thread synchronization, argument passing to threads, and process creation.

Uploaded by

Habib Ur Rehman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <pthread.

h> // For pthread functions


#include <stdio.h> // For standard input/output functions
#include <unistd.h> // For sleep function

// 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);

// Wait for threads to finish


pthread_join(thread1, NULL);
pthread_join(thread2, NULL);

return 0;
}

#include <pthread.h> //p1-pn


#include <stdio.h> // standard input output
#include <unistd.h> // To write the program
#include <stdlib.h>
void *thread_function(void *arg);

int main() {
pthread_t a_thread; //Declare Thread
pthread_create(&a_thread, NULL, thread_function,NULL);
pthread_join(a_thread, NULL); //it waits

printf("Main Program \n");


for(int j=20; j<25; j++) {
printf("%d \n", j);
sleep(1);
}
}
void *thread_function(void *arg) {
printf("Inside the Thread \n");
for (int i=0; i<5; i++) {
printf("%d \n", i);
sleep(1);
}
}

#include <stdio.h> // standard input output


#include <stdlib.h>
#include <unistd.h> // To write the program
#include<sys/types.h>//used for fork

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);
}

#include <pthread.h> // For pthread functions


#include <stdio.h> // For standard input/output functions
#include <unistd.h> // For sleep and getpid
#include <stdlib.h> // For standard library functions
// Define the structure to pass arguments to the thread
struct arg_struct {
int arg1;
int arg2;
};

// Thread function to print arguments


void *arguments(void *arguments) {
struct arg_struct *args = (struct arg_struct *)arguments;
printf("arg1: %d\n", args->arg1);
printf("arg2: %d\n", args->arg2);
pthread_exit(NULL); // Correct function to terminate a thread
return NULL; // Not strictly required after pthread_exit
}

int main() {
pthread_t t; // Thread identifier
struct arg_struct args; // Argument structure

args.arg1 = 5; // Set argument values


args.arg2 = 7;

// Create a new thread


pthread_create(&t, NULL, arguments, &args);

// Wait for the thread to finish


pthread_join(t, NULL);

return 0;
}

You might also like