Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include "a2_helper.h"
- #include <unistd.h>
- #include <sys/types.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <string.h>
- #include <sys/ipc.h>
- #include <sys/sem.h>
- #include <semaphore.h>
- #define THREADSP3 4
- #define MAXTHREADS 4
- #define THREADSP9 49
- #define THREADSP5 5
- int sem_id_p3[THREADSP3];
- int sem_id_p9;
- int sem_id_p5[3];
- int t15s = 0;
- sem_t *sem1;
- sem_t *sem2;
- pthread_mutex_t lock;
- pthread_cond_t cond;
- int nr_of_threads;
- typedef struct _threadName {
- int threadNo;
- int threadId;
- }threadName;
- void P(int semId, int semNr)
- {
- struct sembuf op = { semNr, -1, 0 };
- semop(semId, &op, 1);
- }
- void V(int semId, int semNr)
- {
- struct sembuf op = { semNr, +1, 0 };
- semop(semId, &op, 1);
- }
- void* execThread3(void* threadParam)
- {
- threadName *thName = (threadName *)threadParam;
- //info(BEGIN,thName->threadId, thName->threadNo);
- P(sem_id_p3[thName->threadNo - 1], 0);
- if (thName->threadNo == 3)
- {
- P(sem_id_p5[2], 0);
- }
- info(BEGIN, thName->threadId, thName->threadNo);
- if (thName->threadNo == THREADSP3)
- {
- info(END, thName->threadId, thName->threadNo);
- V(sem_id_p3[THREADSP3 - 2], 0);
- }
- else
- {
- V(sem_id_p3[thName->threadNo], 0);
- P(sem_id_p3[thName->threadNo - 1], 0);
- if (thName->threadNo == 3)
- {
- P(sem_id_p5[0], 0);
- }
- info(END, thName->threadId, thName->threadNo);
- if (thName->threadNo == 3)
- {
- V(sem_id_p5[1], 0);
- }
- V(sem_id_p3[thName->threadNo - 2], 0);
- }
- //V(sem_id_p3[(thName->threadNo)%5],0);
- //info(END,thName->threadId, thName->threadNo);
- //semctl(sem_id_p3[0], 0, IPC_RMID, 0);
- //semctl(sem_id_p3[1], 0, IPC_RMID, 0);
- //semctl(sem_id_p3[2], 0, IPC_RMID, 0);
- //semctl(sem_id_p3[3], 0, IPC_RMID, 0);
- return NULL;
- }
- void* execThread5(void* threadParam)
- {
- threadName *thName = (threadName *)threadParam;
- if (thName->threadNo == 5)
- {
- P(sem_id_p5[1], 0);
- }
- info(BEGIN, thName->threadId, thName->threadNo);
- if (thName->threadNo == 4)
- {
- V(sem_id_p5[2], 0);
- }
- if (thName->threadNo == 4)
- {
- V(sem_id_p5[0], 0);
- }
- info(END, thName->threadId, thName->threadNo);
- //semctl(sem_id_p5[0], 0, IPC_RMID, 0);
- //semctl(sem_id_p5[1], 0, IPC_RMID, 0);
- //semctl(sem_id_p5[2], 0, IPC_RMID, 0);
- return NULL;
- }
- void* execThread9(void* threadParam)
- {
- // int nr_of_threads_local;
- int can_close = 0, can_start = 0;
- threadName *thName = (threadName *)threadParam;
- do
- {
- pthread_mutex_lock(&lock);
- if(nr_of_threads < MAXTHREADS)
- {
- can_start = 1;
- nr_of_threads++;
- info(BEGIN, thName->threadId, thName->threadNo);
- if(thName->threadNo == 15)
- {
- t15s = 1;
- }
- }
- pthread_mutex_unlock(&lock);
- }while (! can_start);
- if (thName->threadNo == 15)
- {
- can_close = 0;
- do
- {
- if (pthread_mutex_lock(&lock) != 0)
- {
- printf("Cannot take the lock\n");
- exit(6);
- }
- if(nr_of_threads >= MAXTHREADS)
- {
- t15s = 0;
- can_close = 1;
- nr_of_threads--;
- info(END, thName->threadId, thName->threadNo);
- }
- if (pthread_mutex_unlock(&lock) != 0)
- {
- printf("Cannot release the lock\n");
- exit(5);
- }
- }while (! can_close);
- }
- else
- {
- can_close = 0;
- do{
- pthread_mutex_lock(&lock);
- if(t15s == 1)
- can_close = 0;
- else
- can_close = 1;
- pthread_mutex_unlock(&lock);
- }while(! can_close);
- pthread_mutex_lock(&lock);
- info(END, thName->threadId, thName->threadNo);
- nr_of_threads--;
- pthread_mutex_unlock(&lock);
- }
- // if (pthread_cond_broadcast(&cond) != 0)
- // {
- // printf("Cannot wait for condition\n");
- // exit(4);
- // }
- // if (pthread_mutex_unlock(&lock) != 0)
- // {
- // printf("Cannot release the lock\n");
- // exit(5);
- // }
- return NULL;
- }
- void process2()
- {
- info(BEGIN, 2, 0);
- info(END, 2, 0);
- }
- void process3()
- {
- //threads test
- pthread_t th[THREADSP3];
- threadName *thName;
- int* state[MAXTHREADS];
- thName = (threadName *)malloc(sizeof(threadName) * THREADSP3);
- for (int i = 0; i < THREADSP3; i++)
- {
- sem_id_p3[i] = semget(IPC_PRIVATE, 1, IPC_CREAT | 0600);
- semctl(sem_id_p3[i], 0, SETVAL, 0);
- if (sem_id_p3[i] < 0) {
- perror("Error creating the semaphore set");
- exit(2);
- }
- thName[i].threadNo = i + 1;
- thName[i].threadId = 3;
- if (pthread_create(&th[i], NULL, execThread3, (void *)(&thName[i])) != 0)
- {
- exit(10);
- }
- }
- V(sem_id_p3[0], 0);
- for (int i = 0; i < THREADSP3; i++)
- {
- pthread_join(th[i], (void**)&state[i]);
- }
- //waitpid(childPid6,NULL,0);
- }
- void process5()
- {
- pthread_t th[THREADSP5];
- int* state[THREADSP5];
- threadName *thName;
- thName = (threadName *)malloc(sizeof(threadName) * THREADSP5);
- for (int i = 0; i < THREADSP5; i++)
- {
- thName[i].threadNo = i + 1;
- thName[i].threadId = 5;
- if (pthread_create(&th[i], NULL, execThread5, (void *)(&thName[i])) != 0)
- {
- exit(10);
- }
- }
- for (int i = 0; i < THREADSP5; i++)
- {
- pthread_join(th[i], (void**)&state[i]);
- }
- }
- void process9()
- {
- //threads test
- pthread_t th[THREADSP9];
- threadName *thName;
- int* state[THREADSP9];
- thName = (threadName *)malloc(sizeof(threadName) * THREADSP9);
- info(BEGIN, 9, 0);
- //sem_id_p9 = semget(IPC_PRIVATE, 1, IPC_CREAT | 0600);
- //semctl(sem_id_p9, 0, SETVAL, 4);
- /*if (sem_id_p9 < 0) {
- perror("Error creating the semaphore set");
- exit(2);
- }
- */
- if (pthread_mutex_init(&lock, NULL) != 0)
- {
- printf("Cannot initialize the lock\n");
- exit(3);
- }
- printf("1\n");
- // if (pthread_cond_init(&cond, NULL) != 0)
- // {
- // printf("Cannot initialize the cond variable\n");
- // exit(4);
- // }
- for (int i = 0; i < THREADSP9; i++)
- {
- thName[i].threadNo = i + 1;
- thName[i].threadId = 9;
- if (pthread_create(&th[i], NULL, execThread9, (void *)(&thName[i])) != 0)
- {
- exit(10);
- }
- }
- for (int i = 0; i < THREADSP9; i++)
- {
- pthread_join(th[i], (void**)&state[i]);
- }
- info(END, 9, 0);
- }
- void initSemahores()
- {
- for (int k = 0; k< 3; k++)
- {
- sem_id_p5[k] = semget(IPC_PRIVATE, 1, IPC_CREAT | 0600);
- semctl(sem_id_p5[k], 0, IPC_RMID, 0);
- semctl(sem_id_p5[k], 0, SETVAL, 0);
- if (sem_id_p5[k] < 0) {
- perror("Error creating the semaphore set");
- exit(2);
- }
- }
- }
- int main() {
- int childPid1 = 0;
- int childPid2 = 0;
- int childPid3 = 0;
- int childPid4 = 0;
- int childPid5 = 0;
- int childPid6 = 0;
- int childPid7 = 0;
- int childPid8 = 0;
- //sem1 = semop("Sem1", O_CREAT | O_EXCL, 0644,)
- initSemahores();
- init();
- info(BEGIN, 1, 0);
- childPid1 = fork();
- if (childPid1 < 0)
- {
- //error exit(1);
- }
- else
- {
- if (childPid1 > 0)
- {
- childPid2 = fork();
- if (childPid2 < 0)
- {
- //error exit(2);
- }
- else
- {
- if (childPid2 >0)
- {
- childPid3 = fork();
- if (childPid3 < 0)
- {
- //error exit(3);
- }
- else
- {
- if (childPid3 >0)
- {
- childPid4 = fork();
- if (childPid4 < 0)
- {
- //error exit(4);
- }
- else
- {
- if (childPid4 >0)
- {
- waitpid(childPid1, NULL, 0);
- waitpid(childPid2, NULL, 0);
- waitpid(childPid3, NULL, 0);
- waitpid(childPid4, NULL, 0);
- info(END, 1, 0);
- }
- else
- {
- info(BEGIN, 7, 0);
- info(END, 7, 0);
- }
- }
- }
- else
- {
- info(BEGIN, 3, 0);
- childPid6 = fork();
- if (childPid6 < 0)
- {
- //error exit(6);
- }
- else
- {
- if (childPid6 >0)
- {
- process3();
- waitpid(childPid6, NULL, 0);
- info(END, 3, 0);
- }
- else
- {
- info(BEGIN, 4, 0);
- childPid7 = fork();
- if (childPid7 < 0)
- {
- //error exit(7);
- }
- else
- {
- if (childPid7 >0)
- {
- childPid8 = fork();
- if (childPid8 < 0)
- {
- //error exit(8);
- }
- else
- {
- if (childPid8 >0)
- {
- //info(BEGIN, 4, 0);
- waitpid(childPid7, NULL, 0);
- waitpid(childPid8, NULL, 0);
- info(END, 4, 0);
- }
- else
- {
- printf("21312312\n" );
- process9();
- // info(BEGIN, 9, 0);
- // info(END, 9, 0);
- }
- }
- }
- else
- {
- info(BEGIN, 6, 0);
- info(END, 6, 0);
- }
- }
- }
- }
- }
- }
- }
- else
- {
- info(BEGIN, 5, 0);
- childPid5 = fork();
- if (childPid5 < 0)
- {
- //error exit(5);
- }
- else
- {
- if (childPid5 >0)
- {
- process5();
- waitpid(childPid5, NULL, 0);
- info(END, 5, 0);
- }
- else
- {
- info(BEGIN, 8, 0);
- info(END, 8, 0);
- }
- }
- }
- }
- }
- else
- {
- process2();
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement