Skip to content

Commit

Permalink
Source Code
Browse files Browse the repository at this point in the history
  • Loading branch information
HasanYahya101 committed Sep 27, 2024
1 parent 411f8df commit 32abdf2
Show file tree
Hide file tree
Showing 18 changed files with 624 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Lab_05/Question_01/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
all: main sender receiver

main: main.c
gcc -o main main.c

sender: sender.c
gcc -o sender sender.c

receiver: receiver.c
gcc -o receiver receiver.c

clean:
rm -f main sender receiver
42 changes: 42 additions & 0 deletions Lab_05/Question_01/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int main()
{
pid_t pid1, pid2;
pid1 = fork();

if (pid1 < 0)
{
printf("Fork for sender failed");
exit(1);
}

if (pid1 == 0)
{
execlp("./sender", "sender", NULL);
exit(0);
}
pid2 = fork();

if (pid2 < 0)
{
printf("Fork for receiver failed");
exit(1);
}

if (pid2 == 0)
{
execlp("./receiver", "receiver", NULL);
exit(0);
}

wait(NULL);
wait(NULL);

printf("Both sender and receiver have completed.\n");

return 0;
}
33 changes: 33 additions & 0 deletions Lab_05/Question_01/receiver.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>

int main()
{
int fd;
char buffer[256];
mkfifo("/tmp/fifo_pipe", 0666);
printf("Receiver waiting for messages...\n");

while (1)
{
fd = open("/tmp/fifo_pipe", O_RDONLY);
if (fd == -1)
{
printf("Failed to open FIFO for reading");
exit(1);
}

if (read(fd, buffer, 256) > 0)
{
printf("Received: %s", buffer);
}

close(fd);
sleep(1);
}

return 0;
}
30 changes: 30 additions & 0 deletions Lab_05/Question_01/sender.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>

int main()
{
int fd;
char buffer[256];
mkfifo("/tmp/fifo_pipe", 0666);

printf("Sender ready. Enter your message:\n");

while (1)
{
fd = open("/tmp/fifo_pipe", O_WRONLY);
if (fd == -1)
{
printf("Failed to open FIFO for writing");
exit(1);
}
fgets(buffer, 256, stdin);
write(fd, buffer, strlen(buffer) + 1);
close(fd);
}

return 0;
}
13 changes: 13 additions & 0 deletions Lab_05/Question_02/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
all: main logger reader

main: main.c
gcc -o main main.c

logger: logger.c
gcc -o logger logger.c

receiver: reader.c
gcc -o reader reader.c

clean:
rm -f main logger reader
12 changes: 12 additions & 0 deletions Lab_05/Question_02/log.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[2024-09-27 23:26:50] System status: 47% load
[2024-09-27 23:26:55] System status: 44% load
[2024-09-27 23:27:00] System status: 38% load
[2024-09-27 23:27:05] System status: 12% load
[2024-09-27 23:27:10] System status: 0% load
[2024-09-27 23:27:15] System status: 70% load
[2024-09-27 23:27:20] System status: 33% load
[2024-09-27 23:27:25] System status: 6% load
[2024-09-28 00:06:06] System status: 97% load
[2024-09-28 00:06:11] System status: 16% load
[2024-09-28 00:06:16] System status: 54% load
[2024-09-28 00:06:21] System status: 3% load
43 changes: 43 additions & 0 deletions Lab_05/Question_02/logger.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>

void generate_system_status(char *status, size_t len)
{
time_t t = time(NULL);
struct tm *tm_info = localtime(&t);
char timestamp[64];
strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M:%S", tm_info);

int random_val = rand() % 100;
snprintf(status, len, "[%s] System status: %d%% load\n", timestamp, random_val);
}

int main()
{
srand(time(NULL));

int fifo_fd;
char buffer[256];

fifo_fd = open("/tmp/log_fifo", O_WRONLY);
if (fifo_fd == -1)
{
printf("Error opening FIFO");
exit(1);
}

while (1)
{
generate_system_status(buffer, 256);
write(fifo_fd, buffer, strlen(buffer));

sleep(5);
}

close(fifo_fd);
return 0;
}
41 changes: 41 additions & 0 deletions Lab_05/Question_02/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>

int main()
{
mkfifo("/tmp/log_fifo", 0666);

pid_t logger_pid, reader_pid;
logger_pid = fork();
if (logger_pid < 0)
{
printf("Fork failed for logger");
exit(1);
}
else if (logger_pid == 0)
{
execlp("./logger", "logger", NULL);
printf("Failed to exec logger");
exit(1);
}
reader_pid = fork();
if (reader_pid < 0)
{
printf("Fork failed for reader");
exit(1);
}
else if (reader_pid == 0)
{
execlp("./reader", "reader", NULL);
printf("Failed to exec reader");
exit(1);
}
wait(NULL);
wait(NULL);

return 0;
}
45 changes: 45 additions & 0 deletions Lab_05/Question_02/reader.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>

int main()
{
int fifo_fd;
char buffer[256];

fifo_fd = open("/tmp/log_fifo", O_RDONLY);
if (fifo_fd == -1)
{
printf("Error opening FIFO");
exit(1);
}

FILE *log_file = fopen("log.txt", "a");
if (log_file == NULL)
{
printf("Error opening log file");
close(fifo_fd);
exit(1);
}

while (1)
{
int num_bytes = read(fifo_fd, buffer, 256 - 1);
if (num_bytes > 0)
{
buffer[num_bytes] = '\0';
fprintf(log_file, "%s", buffer);
fflush(log_file);
}
else if (num_bytes == -1)
{
printf("Error reading from FIFO");
}
}

fclose(log_file);
close(fifo_fd);
return 0;
}
13 changes: 13 additions & 0 deletions Lab_05/Question_03/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
all: main client server

main: main.c
gcc -o main main.c

client: client.c
gcc -o client client.c

receiver: server.c
gcc -o server server.c

clean:
rm -f main client server
38 changes: 38 additions & 0 deletions Lab_05/Question_03/client.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>

int main()
{
char expression[256];
char result[256];
int server_fd, client_fd;

mkfifo("/tmp/client_fifo", 0666);

while (1)
{
printf("Enter a mathematical expression (or 'exit' to quit): ");
fgets(expression, 256, stdin);
expression[strcspn(expression, "\n")] = 0; // Remove newline character

if (strcmp(expression, "exit") == 0)
{
break;
}
server_fd = open("/tmp/server_fifo", O_WRONLY);
write(server_fd, expression, strlen(expression) + 1);
close(server_fd);

client_fd = open("/tmp/client_fifo", O_RDONLY);
read(client_fd, result, 256); // Read result from server
close(client_fd);

printf("Result: %s\n", result);
}

unlink("/tmp/client_fifo");
return 0;
}
48 changes: 48 additions & 0 deletions Lab_05/Question_03/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main()
{
mkfifo("/tmp/server_fifo", 0666);
mkfifo("/tmp/client_fifo", 0666);

pid_t server_pid, client_pid;
server_pid = fork();
if (server_pid < 0)
{
printf("Fork failed for server");
exit(1);
}
else if (server_pid == 0)
{
// Child process runs the server
execlp("./server", "server", NULL);
printf("Failed to exec server");
exit(1);
}

client_pid = fork();
if (client_pid < 0)
{
printf("Fork failed for client");
exit(1);
}
else if (client_pid == 0)
{
execlp("./client", "client", NULL);
printf("Failed to exec client");
exit(1);
}
wait(NULL);
wait(NULL);

// Cleanup
unlink("/tmp/server_fifo");
unlink("/tmp/client_fifo");

return 0;
}
Loading

0 comments on commit 32abdf2

Please sign in to comment.