









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Topics covered in this lecture handout are:Topics covered in this lecture handout are: Case, Study, Problem, Description, Process, Parallel, Multi Threads, Semaphores.
Typology: Study notes
1 / 17
This page cannot be seen from the preview
Don't miss anything!










#include <sys/types.h> #include <stdio.h> #include <errno.h> #include <string.h> #include <stdlib.h> 1
if ((fp=fopen(filename,"w"))==NULL){ perror(filename); exit(1); } if(fprintf(fp,"%d\n",counter)<0) { printf("integer not written.\n"); exit(1); } if(fclose(fp)!=0) { perror(filename); exit(1); } return 0; } int main(int argc, char *argv[]) { int counter; int loop_nr, i; if (argc==1 || sscanf(argv[1],"%d",&loop_nr)!=1) { printf("usage: %s nr\n\t- where nr is an integer\n",argv[0]); exit(1);
printf("Looping %d times\n", loop_nr); for (i=0; i<loop_nr; i++) { counter=read_counter(COUNTER_FILE); printf("read counter: %d\n", counter); sleep(SLEEP_TIME); counter++; write_counter(COUNTER_FILE,counter); printf("write counter: %d\n", counter); sleep(SLEEP_TIME); } }
int main(int argc, char *argv[]) { int counter; int loop_nr, i; if (argc==1 || sscanf(argv[1],"%d",&loop_nr)!=1){ printf("usage: %s nr\n\t- where nr
write_counter(COUNTER_FILE,counter); printf("child write counter: %d\n", counter); sleep(SLEEP_TIME); } } } }
sem_t bin_sem; int counter; void *thread_function(void *arg) { sem_wait(&bin_sem); while(1){ counter=read_counter(COUNTER_FILE); printf("read counter: %d\n", counter); counter++; write_counter(COUNTER_FILE,counter);
printf("write counter: %d\n", counter); sem_wait(&bin_sem); } pthread_exit(NULL); }
int main(int argc, char *argv[]) { int res; int loop_nr, i; pthread_t thread; if (argc==1 || sscanf(argv[1],"%d",&loop_nr)!=1){ printf("usage: %s nr\n\t- where nr is an integer\n",argv[0]); exit(EXIT_FAILURE); } res = sem_init(&bin_sem, 0, 0); if(res != 0){ printf("Failed to initialize semaphore\n"); exit(EXIT_FAILURE); } res=pthread_create(&thread, NULL, thread_funct
#include <pthread.h> #include <stdio.h> #include <sys/types.h> #include <stdlib.h> #include <unistd.h>
#define NMAX 50 2
pthread_t threads[NMAX];
typedef struct{ int number; }phil_data;
phil_data phil_data_tab[NMAX];
void *phil(void arg) { int my_number; my_number= ((phil_data)arg)-> number; printf("Thread %d started\n", my_number); } int main(int *agrc, char *argv[]) { int i, n; n= atoi(argv[1]);
for(i= 0; i<n; i++){ phil_data_tab[i].number = i; pthread_create(&(threads[i]), 0, phil,
my_number = (int)arg; printf("Thread %d started\n", my_number); } int main(int argc, char *argv[]) { int i, n; if(argc < 2) printf("Incorrect number of argument"); n = atoi(argv[1]);
for(i=0; i<n; i++){ pthread_create(&threads[i], NULL, new_thread, &i); } pthread_detach(threads[i]); pthread_exit(0); }
#define NMAX 50
/* declare an array of forks, returns 1 if forks available, 0 if not */ static int forks[NMAX]; pthread_mutex_t mutex;
/this function grab a single fork/ int grab(int fork_number) { pthread_mutex_lock(&mutex); /* wait for fork to become available/ while(forks[fork_number]!=1){ printf("philosopher %d fail to grab fork %d.\n",my_number,fork_number); pthread_mutex_unlock(&mutex); usleep(THINK_TIME); pthread_mutex_lock(&mutex); } / grab fork, set it as unavailable*/ forks[fork_number]= 0; pthread_mutex_unlock(&mutex); }
/this function release a single fork/
my_number); sleep(EAT_TIME); release(my_number); release((my_number+1)%NMAX); printf("Philosopher %d starts to think.\n", my_number); sleep(THINK_TIME); }
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <pthread.h> #include <semaphore.h> #include <sys/types.h> int main(int *agrc, char *argv[]) { int res; pthread_t threads[NMAX]; int i, n;
n= atoi(argv[1]); for (i=0; i<n; i++){ forks[i]=1; /* initialize forks*/ res= sem_init(&bin_sem[i], 0, 0); if (res!=0){ perror("semaphore failed to initialized.\n"); exit(EXIT_FAILURE); } res = pthread_create(&threads[i], 0, phil, &i); if (res!=0){ perror("thread creation failed.\n"); exit(EXIT_FAILURE); } sem_post(&bin_sem[i]); } pthread_detach(threads[i]); sem_destroy(&bin_sem[i]); exit(EXIT_SUCCESS); }