From 0918a5cf8483541b94f123f8a8b338d11fc34e9e Mon Sep 17 00:00:00 2001 From: Ziad Arafat Date: Sun, 26 Sep 2021 11:49:28 -0600 Subject: [PATCH] removed old files we moved to the folder --- Program1_5/prog1.c | 136 ---------------------------------------- Program1_5/shm_client.c | 56 ----------------- Program1_5/shm_server.c | 64 ------------------- 3 files changed, 256 deletions(-) delete mode 100644 Program1_5/prog1.c delete mode 100644 Program1_5/shm_client.c delete mode 100644 Program1_5/shm_server.c diff --git a/Program1_5/prog1.c b/Program1_5/prog1.c deleted file mode 100644 index 46030c3..0000000 --- a/Program1_5/prog1.c +++ /dev/null @@ -1,136 +0,0 @@ -// Ziad Arafat -// Username: zarafat -// Oct 4 2020 - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -// create key for shared memory -#define SHMKEY ( ( key_t )1497 ) - -// define struct to store value in -typedef struct { - int value; -} shared_mem; - -// initialize memory -static shared_mem *total; - - -// Process 1 to 4. -// Tagged with noreturn because we are always exiting before returning anything. -static noreturn void process1( ) { - // keep incrementing value until we reach the value we want - while ( total->value < 100000 ) { - total->value++; - } - - // print out what the value is after we are done incrementing. - printf( "Process 1: Value = %d\n", total->value ); - - // exit the child process - exit( 0 ); -} - -static noreturn void process2( ) { - while ( total->value < 200000 ) { - total->value++; - } - - printf( "Process 2: Value = %d\n", total->value ); - - exit( 0 ); -} - -static noreturn void process3( ) { - while ( total->value < 300000 ) { - total->value++; - } - - printf( "Process 3: Value = %d\n", total->value ); - - exit( 0 ); -} - -static noreturn void process4( ) { - while ( total->value < 500000 ) { - total->value++; - } - - printf( "Process 4: Value = %d\n", total->value ); - - exit( 0 ); -} - -int main( ) { - int shmid, pid1, pid2, pid3, pid4; - - char *shmadd; - - shmadd = ( char * )0; - - if ( ( shmid = shmget( SHMKEY, sizeof( int ), IPC_CREAT | 0666 ) ) < - 0 ) { - perror( "shmget" ); - exit( 1 ); - } - - if ( ( total = ( shared_mem * )shmat( shmid, shmadd, 0 ) ) == - ( shared_mem * )-1 ) { - perror( "shmat" ); - exit( 0 ); - } - - total->value = 0; - - int childcnt = 0; - - if ( ( pid1 = fork( ) ) == 0 ) { - process1( ); - } - childcnt++; - if ( ( pid2 = fork( ) ) == 0 ) { - process2( ); - } - childcnt++; - if ( ( pid3 = fork( ) ) == 0 ) { - process3( ); - } - childcnt++; - if ( ( pid4 = fork( ) ) == 0 ) { - process4( ); - } - childcnt++; - - - while ( childcnt > 0 ) { - int pid = wait( NULL ); - if ( pid == pid1 ) { - printf( "Process 1 PID %d finished\n", pid1 ); - } - else if ( pid == pid2 ) { - printf( "Process 2 PID %d finished\n", pid2 ); - } - else if ( pid == pid3 ) { - printf( "Process 3 PID %d finished\n", pid3 ); - } - else if ( pid == pid4 ) { - printf( "Process 4 PID %d finished\n", pid4 ); - } - childcnt--; - } - - if ( shmdt( total ) == -1 ) { - perror( "shmdt" ); - - exit( -1 ); - } -} \ No newline at end of file diff --git a/Program1_5/shm_client.c b/Program1_5/shm_client.c deleted file mode 100644 index e851ed1..0000000 --- a/Program1_5/shm_client.c +++ /dev/null @@ -1,56 +0,0 @@ -/* - * shm-client - client program to demonstrate shared memory. - */ -#include -#include -#include -#include -#include - -#define SHMSZ 27 - -main() -{ - int shmid; - key_t key; - char *shm, *s; - - /* - * We need to get the segment named - * "5678", created by the server. - */ - key = 5678; - - /* - * Locate the segment. - */ - if ((shmid = shmget(key, SHMSZ, 0666)) < 0) { - perror("shmget"); - exit(1); - } - - /* - * Now we attach the segment to our data space. - */ - if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) { - perror("shmat"); - exit(1); - } - - /* - * Now read what the server put in the memory. - */ - for (s = shm; *s != NULL; s++) - putchar(*s); - putchar('\n'); - - /* - * Finally, change the first character of the - * segment to '*', indicating we have read - * the segment. - */ - *shm = '*'; - - exit(0); -} - diff --git a/Program1_5/shm_server.c b/Program1_5/shm_server.c deleted file mode 100644 index a8f75aa..0000000 --- a/Program1_5/shm_server.c +++ /dev/null @@ -1,64 +0,0 @@ -#include -#include -#include -#include -#include - -#define SHMSZ 27 - -main() -{ - char c; - int shmid; - key_t key; - char *shm, *s; - - struct shmid_ds shmds; // added so that we can remove the structure after we run - - /* - * We'll name our shared memory segment - * "5678". - */ - key = 5678; - - /* - * Create the segment. - */ - if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) { - perror("shmget"); - exit(1); - } - - /* - * Now we attach the segment to our data space. - */ - if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) { - perror("shmat"); - exit(1); - } - - /* - * Now put some things into the memory for the - * other process to read. - */ - s = shm; - - for (c = 'a'; c <= 'z'; c++) - *s++ = c; - *s = NULL; - - /* - * Finally, we wait until the other process - * changes the first character of our memory - * to '*', indicating that it has read what - * we put there. - */ - while (*shm != '*') - sleep(1); - - /* now we tell the OS to remove the segment */ - - shmctl(shmid,IPC_RMID,&shmds); - exit(0); -} -