From 709b5cf1d7830ef8f9953aae3b504f1851212ffb Mon Sep 17 00:00:00 2001 From: Ziad Arafat Date: Sun, 26 Sep 2021 11:49:08 -0600 Subject: [PATCH] moved example files to a folder --- Program1_5/files_from_os/prog1.c | 136 ++++++++++++++++++++++++++ Program1_5/files_from_os/shm_client.c | 56 +++++++++++ Program1_5/files_from_os/shm_server.c | 64 ++++++++++++ 3 files changed, 256 insertions(+) create mode 100644 Program1_5/files_from_os/prog1.c create mode 100644 Program1_5/files_from_os/shm_client.c create mode 100644 Program1_5/files_from_os/shm_server.c diff --git a/Program1_5/files_from_os/prog1.c b/Program1_5/files_from_os/prog1.c new file mode 100644 index 0000000..46030c3 --- /dev/null +++ b/Program1_5/files_from_os/prog1.c @@ -0,0 +1,136 @@ +// 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/files_from_os/shm_client.c b/Program1_5/files_from_os/shm_client.c new file mode 100644 index 0000000..e851ed1 --- /dev/null +++ b/Program1_5/files_from_os/shm_client.c @@ -0,0 +1,56 @@ +/* + * 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/files_from_os/shm_server.c b/Program1_5/files_from_os/shm_server.c new file mode 100644 index 0000000..a8f75aa --- /dev/null +++ b/Program1_5/files_from_os/shm_server.c @@ -0,0 +1,64 @@ +#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); +} +