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 <pthread.h>
- #include <fcntl.h> /* For O_* constants */
- #include <sys/stat.h> /* For mode constants */
- #include <semaphore.h>
- #include "a2_helper.h"
- sem_t *s3, *s4, *s5;
- sem_t sem, s1, s2;
- pthread_t t3_1, t3_2, t3_3, t3_4, t3_5, t7_1, t7_2, t7_3, t7_4, t7_5, t2[43];
- void *T3_1(void *arg)
- {
- sem_wait(s3);
- info(BEGIN, 3, 1);
- info(END, 3, 1);
- sem_post(s5);
- pthread_exit(NULL);
- }
- void *T3_2(void *arg)
- {
- info(BEGIN, 3, 2);
- info(END, 3, 2);
- pthread_exit(NULL);
- }
- void *T3_3(void *arg)
- {
- sem_wait(&s1);
- info(BEGIN, 3, 3);
- info(END, 3, 3);
- sem_post(&s2);
- pthread_exit(NULL);
- }
- void *T3_4(void *arg)
- {
- info(BEGIN, 3, 4);
- sem_post(&s1);
- sem_wait(&s2);
- pthread_join(t3_3, NULL);
- info(END, 3, 4);
- sem_destroy(&s1);
- sem_destroy(&s2);
- pthread_exit(NULL);
- }
- void *T3_5(void *arg)
- {
- info(BEGIN, 3, 5);
- info(END, 3, 5);
- pthread_exit(NULL);
- }
- void *T7_1(void *arg)
- {
- info(BEGIN, 7, 1);
- info(END, 7, 1);
- pthread_exit(NULL);
- }
- void *T7_2(void *arg)
- {
- info(BEGIN, 7, 2);
- info(END, 7, 2);
- sem_post(s3);
- pthread_exit(NULL);
- }
- void *T7_3(void *arg)
- {
- sem_wait(s5);
- info(BEGIN, 7, 3);
- info(END, 7, 3);
- pthread_exit(NULL);
- }
- void *T7_4(void *arg)
- {
- info(BEGIN, 7, 4);
- info(END, 7, 4);
- pthread_exit(NULL);
- }
- void *T7_5(void *arg)
- {
- info(BEGIN, 7, 5);
- info(END, 7, 5);
- pthread_exit(NULL);
- }
- void *thread(void *arg)
- {
- int id = (intptr_t)arg;
- info(BEGIN, 4, id);
- info(END, 4, id);
- pthread_exit(NULL);
- }
- int main()
- {
- pid_t p2 = -1, p3 = -1, p4 = -1, p5 = -1, p6 = -1, p7 = -1;
- init();
- info(BEGIN, 1, 0);
- s3 = sem_open("/semafor1", O_CREAT, 0644, 0);
- s5 = sem_open("/semafor3", O_CREAT, 0644, 0);
- p2 = fork();
- if (p2 == 0)
- {
- info(BEGIN, 2, 0);
- info(END, 2, 0);
- }
- else
- {
- p3 = fork();
- if (p3 == 0)
- {
- info(BEGIN, 3, 0);
- sem_init(&s1, 0, 0);
- sem_init(&s2, 0, 0);
- pthread_create(&t3_4, NULL, &T3_4, (void *)4);
- pthread_create(&t3_3, NULL, &T3_3, (void *)3);
- pthread_create(&t3_1, NULL, &T3_1, (void *)1);
- pthread_create(&t3_5, NULL, &T3_5, (void *)5);
- pthread_create(&t3_2, NULL, &T3_2, (void *)2);
- pthread_join(t3_3, NULL);
- pthread_join(t3_4, NULL);
- pthread_join(t3_1, NULL);
- pthread_join(t3_5, NULL);
- pthread_join(t3_2, NULL);
- info(END, 3, 0);
- }
- else
- {
- p4 = fork();
- if (p4 == 0)
- {
- info(BEGIN, 4, 0);
- // for (int i = 0; i < 43; i++)
- //{
- // pthread_create(&t2[i], NULL, &thread, (void *)(intptr_t)i + 1);
- // }
- // for (int i = 0; i < 43; i++)
- // {
- // pthread_join(t2[i], NULL);
- // }
- info(END, 4, 0);
- }
- else
- {
- p5 = fork();
- if (p5 == 0)
- {
- info(BEGIN, 5, 0);
- info(END, 5, 0);
- }
- else
- {
- p6 = fork();
- if (p6 == 0)
- {
- info(BEGIN, 6, 0);
- info(END, 6, 0);
- }
- else
- {
- p7 = fork();
- if (p7 == 0)
- {
- info(BEGIN, 7, 0);
- pthread_create(&t7_4, NULL, &T7_4, (void *)4);
- pthread_create(&t7_3, NULL, &T7_3, (void *)3);
- pthread_create(&t7_1, NULL, &T7_1, (void *)1);
- pthread_create(&t7_5, NULL, &T7_5, (void *)5);
- pthread_create(&t7_2, NULL, &T7_2, (void *)2);
- pthread_join(t7_3, NULL);
- pthread_join(t7_4, NULL);
- pthread_join(t7_1, NULL);
- pthread_join(t7_5, NULL);
- pthread_join(t7_2, NULL);
- info(END, 7, 0);
- }
- else
- {
- wait(NULL);
- wait(NULL);
- wait(NULL);
- wait(NULL);
- wait(NULL);
- wait(NULL);
- info(END, 1, 0);
- sem_close(s3);
- sem_close(s5);
- }
- }
- }
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement