Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<pthread.h>
- #include<semaphore.h>
- #include<unistd.h>
- int oPush = 0, oPushA = 0, oPushB = 0, oPull =0; //0 mean
- int amountSuccess = 0;
- int amountErrors = 0;
- int amountCommit = 0;
- pthread_mutex_t working = PTHREAD_MUTEX_INITIALIZER;
- pthread_mutex_t deadlockMutex = PTHREAD_MUTEX_INITIALIZER;
- pthread_cond_t pulling = PTHREAD_COND_INITIALIZER;
- sem_t pushA,pushB,mutex;
- void *PersonA_DeadLock(void *temp) {
- int i;
- for(i = 0;i<2;i++)
- {
- printf("A trying to pull file from the GIT\n");
- sem_wait(&mutex);
- printf("Pulling Success\n");
- sem_wait(&pushB);
- printf("A:%d\n",i);
- printf("A is working on the code.\n");
- sleep(1);
- printf("A is trying to commit to the GIT system\n");
- sem_post(&pushA);
- printf("Commit Success\n");
- sem_post(&mutex);
- }
- }
- void *PersonB_DeadLock(void *temp) {
- int i;
- for(i = 0;i<2;i++)
- {
- printf("A trying to pull file from the GIT\n");
- sem_wait(&mutex);
- printf("Pulling Success\n");
- sem_wait(&pushA);
- printf("B:%d\n",i);
- printf("B is working on the code.\n");
- sleep(1);
- printf("B is trying to commit to the GIT system\n");
- sem_post(&pushB);
- printf("Commit Success\n");
- sem_post(&mutex);
- }
- printf("B Commit >= %d, %d, %d\n", amountCommit, amountSuccess, amountErrors);
- }
- void *PersonA_UnDeadLock(void *temp) {
- int i;
- for(i = 0;i<2;i++)
- {
- printf("A trying to pull file from the GIT\n");
- sem_wait(&pushB);
- printf("Pulling Success\n");
- sem_wait(&mutex);
- printf("A:%d\n",i);
- printf("A is working on the code.\n");
- sleep(1);
- printf("A is trying to commit to the GIT system\n");
- amountCommit = amountCommit +1;
- sem_post(&mutex);
- amountSuccess = amountSuccess + 1;
- printf("Commit Success\n");
- sem_post(&pushA);
- }
- printf("A Commit >= %d, %d, %d\n", amountCommit, amountSuccess, amountErrors);
- }
- void *PersonB_UnDeadLock(void *temp) {
- int i;
- for(i = 0;i<2;i++)
- {
- printf("A trying to pull file from the GIT\n");
- sem_wait(&pushA);
- printf("Pulling Success\n");
- sem_wait(&mutex);
- printf("B:%d\n",i);
- printf("B is working on the code.\n");
- sleep(1);
- printf("B is trying to commit to the GIT system\n");
- amountCommit = amountCommit +1;
- sem_post(&mutex);
- printf("Commit Success\n");
- amountSuccess = amountSuccess +1;
- sem_post(&pushB);
- }
- printf("B Commit >= %d, %d, %d\n", amountCommit, amountSuccess, amountErrors);
- }
- int main() {
- sem_init(&mutex, 0, 1);
- sem_init(&pushB, 0, 1);
- pthread_t t[2];
- pthread_create(&t[0], NULL, &PersonA_DeadLock, "PersonA");
- pthread_create(&t[1], NULL, &PersonB_DeadLock, "PersonB");
- pthread_join(t[1], NULL);
- pthread_join(t[0], NULL);
- sem_destroy(&mutex);
- sem_destroy(&pushB);
- sem_destroy(&pushA);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement