Skip to content

Commit

Permalink
Source Code
Browse files Browse the repository at this point in the history
  • Loading branch information
HasanYahya101 committed Dec 6, 2024
1 parent 13cad75 commit 785459c
Show file tree
Hide file tree
Showing 4 changed files with 254 additions and 0 deletions.
152 changes: 152 additions & 0 deletions Lab_14/Question_01/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <fcntl.h>
#include <sys/wait.h>

union semun
{
int val;
struct semid_ds *buf;
unsigned short *array;
};

int init_semaphores()
{
key_t key = ftok(".", 'S');
int semid = semget(key, 2, IPC_CREAT | 0666);

union semun sem_arg;
sem_arg.val = 1;
semctl(semid, 0, SETVAL, sem_arg);

sem_arg.val = 0;
semctl(semid, 1, SETVAL, sem_arg);

return semid;
}

// P1
void calc_sum(int semid)
{
struct sembuf sb = {0, -1, 0}; // Wait operation
semop(semid, &sb, 1); // Lock file access

FILE *file = fopen("numbers.txt", "r+");
if (file == NULL)
{
printf("File opening failed");
exit(1);
}

int num, sum = 0;
while (fscanf(file, "%d", &num) == 1)
{
sum += num;
}

// Write sum at the end of file
fseek(file, 0, SEEK_END);
fprintf(file, "\nSum: %d", sum);
fclose(file);

sb.sem_num = 1;
sb.sem_op = 1; // Signal P2 to start
semop(semid, &sb, 1);

printf("P1: Sum calculation completed\n");
}

// P2
void count_int(int semid)
{
struct sembuf sb = {1, -1, 0}; // Wait for P1
semop(semid, &sb, 1);

sb.sem_num = 0;
sb.sem_op = -1; // Lock file access
semop(semid, &sb, 1);

FILE *file = fopen("numbers.txt", "r+");
if (file == NULL)
{
printf("File opening failed");
exit(1);
}

int num, count = 0;
while (fscanf(file, "%d", &num) == 1)
{
count++;
}

fseek(file, 0, SEEK_END);
fprintf(file, "\nCount: %d", count);
fclose(file);

sb.sem_num = 1;
sb.sem_op = 1; // Signal P3 to start
semop(semid, &sb, 1);

printf("P2: Count calculation completed\n");
}

// P3
void calc_avg(int semid)
{
struct sembuf sb = {1, -1, 0}; // Wait for P2
semop(semid, &sb, 1);

FILE *file = fopen("numbers.txt", "r");
if (file == NULL)
{
printf("File opening failed");
exit(1);
}

int sum, count;
char line[100];
while (fgets(line, sizeof(line), file))
{
if (sscanf(line, "Sum: %d", &sum) == 1)
continue;
if (sscanf(line, "Count: %d", &count) == 1)
break;
}
fclose(file);

float average = (float)sum / count;
printf("P3: Average = %.2f\n", average);

semctl(semid, 0, IPC_RMID, 0);
}

int main()
{
int semid = init_semaphores();
pid_t pid1, pid2;

pid1 = fork();
if (pid1 == 0)
{
calc_sum(semid);
exit(0);
}

pid2 = fork();
if (pid2 == 0)
{
count_int(semid);
exit(0);
}

calc_avg(semid); // P3

wait(NULL);
wait(NULL);

return 0;
}
12 changes: 12 additions & 0 deletions Lab_14/Question_01/numbers.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
1
2
3
4
5
6
7
8
Sum: 36
Count: 8
Sum: 36
Count: 8
81 changes: 81 additions & 0 deletions Lab_14/Question_02/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdlib.h>
#include <unistd.h>

sem_t sem_S1_done;
sem_t sem_S2_done;
sem_t log_mutex;

FILE *log_file;

void *service_S1(void *arg)
{
sem_wait(&log_mutex);

printf("S1: Placing order\n");
log_file = fopen("shared_log.txt", "a");
fprintf(log_file, "Order ID: 1234, Customer: John Doe\n");
fclose(log_file);

sem_post(&log_mutex);
sem_post(&sem_S1_done);

return NULL;
}

void *service_S2(void *arg)
{
sem_wait(&sem_S1_done);
sem_wait(&log_mutex);

printf("S2: Processing payment\n");
log_file = fopen("shared_log.txt", "a");
fprintf(log_file, "Payment Status: Successful\n");
fclose(log_file);

sem_post(&log_mutex);
sem_post(&sem_S2_done);

return NULL;
}

void *service_S3(void *arg)
{
sem_wait(&sem_S2_done);
sem_wait(&log_mutex);

printf("S3: Confirming order\n");
log_file = fopen("shared_log.txt", "a");
fprintf(log_file, "Order Confirmation: Confirmed, Timestamp: %ld\n", time(NULL));
fclose(log_file);

sem_post(&log_mutex);

return NULL;
}

int main()
{
sem_init(&sem_S1_done, 0, 0);
sem_init(&sem_S2_done, 0, 0);
sem_init(&log_mutex, 0, 1);

pthread_t thread_S1, thread_S2, thread_S3;

pthread_create(&thread_S1, NULL, service_S1, NULL);
pthread_create(&thread_S2, NULL, service_S2, NULL);
pthread_create(&thread_S3, NULL, service_S3, NULL);

pthread_join(thread_S1, NULL);
pthread_join(thread_S2, NULL);
pthread_join(thread_S3, NULL);

sem_destroy(&sem_S1_done);
sem_destroy(&sem_S2_done);
sem_destroy(&log_mutex);

printf("All services completed successfully.\n");
return 0;
}
9 changes: 9 additions & 0 deletions Lab_14/Question_02/shared_log.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Order ID: 1234, Customer: John Doe
Payment Status: Successful
Order Confirmation: Confirmed, Timestamp: 1733469475
Order ID: 1234, Customer: John Doe
Payment Status: Successful
Order Confirmation: Confirmed, Timestamp: 1733469594
Order ID: 1234, Customer: John Doe
Payment Status: Successful
Order Confirmation: Confirmed, Timestamp: 1733469612

0 comments on commit 785459c

Please sign in to comment.