-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJay Master-Slave.c
More file actions
70 lines (55 loc) · 2.76 KB
/
Jay Master-Slave.c
File metadata and controls
70 lines (55 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* Includes */
#include <unistd.h> /* Symbolic Constants */
#include <sys/types.h> /* Primitive System Data Types */
#include <errno.h> /* Errors */
#include <stdio.h> /* Input/Output */
#include <stdlib.h> /* General Utilities */
#include <pthread.h> /* POSIX Threads */
#include <string.h> /* String handling */
#include <semaphore.h> /* Semaphore */
/* prototype for worker thread routine */
void workerjob ( void *ptr );
/* global vars */
unsigned long x = 0;
unsigned char c;
char str[]="DEVIL RATS ON NO STAR LIVE DEVIL RATS ON NO STAR LIVE DEVIL RATS ON NO STAR LIVE DEVIL RATS ON NO STAR LIVE DEVIL RATS ON NO STAR LIVE DEVIL RATS ON NO STAR LIVE DEVIL RATS ON NO STAR LIVE DEVIL RATS ON NO STAR LIVE DEVIL RATS ON NO STAR LIVE DEVIL RATS ON NO STAR LIVE DEVIL RATS ON NO STAR LIVE DEVIL RATS ON NO STAR LIVE DEVIL RATS ON NO STAR LIVE DEVIL RATS ON NO STAR LIVE DEVIL RATS ON NO STAR LIVE DEVIL RATS ON NO STAR LIVE DEVIL RATS ON NO STAR LIVE DEVIL RATS ON NO STAR LIVE DEVIL RATS ON NO STAR LIVE DEVIL RATS ON NO STAR LIVE DEVIL RATS ON NO STAR LIVE DEVIL RATS ON NO STAR LIVE DEVIL RATS ON NO STAR LIVE DEVIL RATS ON NO STAR LIVE DEVIL RATS ON NO STAR LIVE DEVIL RATS ON NO STAR LIVE DEVIL RATS ON NO STAR LIVE DEVIL RATS ON NO STAR LIVE DEVIL RATS ON NO STAR LIVE DEVIL RATS ON NO STAR LIVE DEVIL RATS ON NO STAR LIVE DEVIL RATS ON NO STAR LIVE DEVIL RATS ON NO STAR LIVE DEVIL RATS ON NO STAR LIVE DEVIL RATS ON NO STAR LIVE ";
/* semaphores are declared global so they can be accessed
in main() and in thread routine,
here, the semaphore is used as a mutex */
sem_t mutex;
int main()
{
int size=strlen(str);
int i[2];
pthread_t worker;
//pthread_t thread_b;
i[0] = 0; /* argument to threads */
i[1] = 1;
sem_init(&mutex, 0, 1); /* initialize mutex to 1 - binary semaphore */
/* second param = 0 - semaphore is local */
printf("String is initially: %s \n", str);
/* Note: you can check if thread has been successfully created by checking return value of pthread_create */
for (x = 0; x < (size / 2); ++x)
{
pthread_create (&worker, NULL, (void *) &workerjob, (void *) &i[0]);
pthread_join(worker, NULL);
}
sem_destroy(&mutex); /* destroy semaphore */
printf("After being reversed, string is: %s \n", str);
/* exit */
return 1;
exit(0);
} /* main() */
void workerjob ( void *ptr )
{
int size=strlen(str);
sem_wait(&mutex); /* down semaphore */
/* START CRITICAL REGION */
c = str[(size - 1)- x];
str[(size - 1) - x] = str[x];
str[x] = c;
printf("String is now: %s \n", str);
/* END CRITICAL REGION */
sem_post(&mutex); /* up semaphore */
pthread_exit(0); /* exit thread */
}