1
0

added more code

This commit is contained in:
Andrew W
2022-08-28 16:12:16 -05:00
parent 5a2894ed1b
commit 7dabaef6f6
2345 changed files with 1343530 additions and 0 deletions

Binary file not shown.

Binary file not shown.

BIN
CPE435/Lab4/Lab04.pdf Normal file

Binary file not shown.

BIN
CPE435/Lab4/Process_A Executable file

Binary file not shown.

73
CPE435/Lab4/Process_A.c Normal file
View File

@ -0,0 +1,73 @@
/***********************************************************************************
Code Inherited from Anon
Massively Modified by Prawar Poudel, 3 Feb 2020 (pp0030@uah.edu)
Modified by Noah Eid, 30 Jan 2021 (nae0005@uah.edu)
************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
#include "line.h"
/*
Process A creates a shared memory segment
Process A attaches a pointer to the shared memory segment created
.. then it writes an integer value and a character value to it
Process A will then print that character every 4 seconds to terminal
.. a number of times
Process B will can also access the shared memory segment and change
.. the character being printed
After the printing operation is done, Process A will detach and delete the shared memory segment
*/
main(void)
{
int i, id ;
struct info *ctrl;
// create the shared memory segment
// .. id is the file identifier of the shared memory segment
// .. if the key is shared, any process can attach to this shared memory segment
// .. 0666 is the file permission for the shared memory segment
if ( (id = shmget( key,MSIZ,IPC_CREAT | 0666) ) < 0 )
{
//error…;
printf("error 1\n");
exit(1) ;
}
// attach a local pointer to the shared memory segment created
// .. exit if fails
if ( (ctrl = (struct info *) shmat( id, 0, 0)) <= (struct info *) (0) )
{
//error … ;
printf("error 2\n");
exit(1) ;
}
// put some initial data in the shared memory so that we will just print the things before ProcessB is run
ctrl->c = 'a';
ctrl->length = 10;
// print line every 4 seconds
for (i = 0 ; i <ctrl->length ; i++ )
{
putchar (ctrl->c);
putchar ('\n');
sleep(4);
}
//now detach the pointer from the shared memory
shmdt(ctrl);
//let us delete the shared memory
shmctl(id,IPC_RMID,NULL);
//job done
}

BIN
CPE435/Lab4/Process_B Executable file

Binary file not shown.

57
CPE435/Lab4/Process_B.c Normal file
View File

@ -0,0 +1,57 @@
/***********************************************************************************
Code Inherited from Anon
Massively Modified by Prawar Poudel, 3 Feb 2020 (pp0030@uah.edu)
Modified by Noah Eid, 30 Jan 2021 (nae0005@uah.edu)
************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include "line.h"
/*
Process B can change the contents of the shared memory that Process A is using
./ProcessB <char> <int>
*/
main(int argc, char *argv[])
{
int id ;
struct info *ctrl;
if (argc < 3)
{
//error…;
exit(3);
}
// get the id of the already created shared memory segment
// .. function to create is same as getting id of the already created shared memory segment
if ( (id = shmget( key,MSIZ, 0 )) < 0 )
{
//error … ;
exit(1) ;
}
// attach a local pointer to the shared memory
ctrl = (struct info *) shmat( id, 0, 0);
if ( ctrl <= (struct info *) (0) )
{
//error … ;
exit(1) ;
}
/* copy command line data to shared memory */
ctrl->c = argv[1][0] ;
ctrl->length = atoi(argv[2]);
//detach the pointer from the shared memory
// .. we do not need to delete here, can be done in either of the process
shmdt(ctrl);
exit(0);
}

89
CPE435/Lab4/Process_C.c Normal file
View File

@ -0,0 +1,89 @@
/***********************************************************************************
Code Inherited from Anon
Massively Modified by Prawar Poudel, 3 Feb 2020 (pp0030@uah.edu)
************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include "header.h"
/*
Process C creates a shared memory segment
Process C attaches a pointer to the shared memory segment created
.. then it writes an integer value and a character value to it
.. it prints the character value for the integer number of times
.. then it waits on Process D to update the value of integer and the character
.. .. this will be indicated by the flag value being set to 1 by Process D
.. after the same printing operation is done, Process C will detach and delete the shared memory segment
*/
main(void)
{
int i, id ;
struct info *ctrl;
// create the shared memory segment
// .. id is the file identifier of the shared memory segment
// .. if the key is shared, any process can attach to this shared memory segment
// .. 0666 is the file permission for the shared memory segment
if ( (id = shmget( key,MSIZ,IPC_CREAT | 0666) ) < 0 )
{
//error…;
printf("error 1\n");
exit(1) ;
}
// attach a local pointer to the shared memory segment created
// .. exit if fails
if ( (ctrl = (struct info *) shmat( id, 0, 0)) <= (struct info *) (0) ){
//error … ;
printf("error 2\n");
exit(1) ; }
// put some initial data in the shared memory so that we will just print the things before Process D is run
ctrl->c = 'a';
ctrl->length = 10;
ctrl->flag = 0;
// print the character ctrl->c for ctrl->length times in a single line
for (i = 0 ; i <ctrl->length ; i++ )
{
putchar (ctrl->c);
putchar (' ');
}
putchar('\n');
// flushing stdout just in case
fflush(stdout);
// signal Process D that Process C is ready for next round, which will be printed below
ctrl->flag = 2;
// wait for the flag to be set in processB to 1
// .. flag is 0 right now
// .. once processB starts to run, it will set the flag to 1
while(ctrl->flag!=1);
// print the character ctrl->c for ctrl->length times in a single line
for (i = 0 ; i <ctrl->length ; i++ )
{
putchar (ctrl->c);
putchar (' ');
}
putchar ('\n');
// flushing stdout just in case
fflush(stdout);
//now detach the pointer from the shared memory
shmdt(ctrl);
//let us delete the shared memory
shmctl(id,IPC_RMID,NULL);
//job done
}

51
CPE435/Lab4/Process_D.c Normal file
View File

@ -0,0 +1,51 @@
/***********************************************************************************
Code Inherited from Anon
Massively Modified by Prawar Poudel, 3 Feb 2020 (pp0030@uah.edu)
************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include "header.h"
main(int argc, char *argv[])
{
int id ;
struct info *ctrl;
if (argc < 3)
{
//error…;
exit(3);
}
// get the id of the already created shared memory segment
// .. function to create is same as getting id of the already created shared memory segment
// .. notice the difference between the flags used in Process C and Process D
if ( (id = shmget( key,MSIZ, 0 )) < 0 )
{
//error … ;
exit(1) ;
}
// attach a local pointer to the shared memory
ctrl = (struct info *) shmat( id, 0, 0);
if ( ctrl <= (struct info *) (0) )
{
//error … ;
exit(1) ;
}
// wait until Process C is ready to receive from process D
while(ctrl->flag==0);
/* copy command line data to shared memory */
ctrl->c = argv[1][0] ;
ctrl->length = atoi(argv[2]);
ctrl->flag = 1;
//detach the pointer from the shared memory
// .. we do not need to delete here, can be done in either of the process
shmdt(ctrl);
exit(0);
}

7
CPE435/Lab4/header.h Normal file
View File

@ -0,0 +1,7 @@
struct info {
char c;
int length;
char flag;
};
key_t key = 1243 ;
#define MSIZ sizeof(struct info)

8
CPE435/Lab4/info.h Normal file
View File

@ -0,0 +1,8 @@
struct info
{
float value1, value2;
float sum;
int flag;
};
#define KEY ((key_t)(1234))
#define MSIZ sizeof(struct info)

6
CPE435/Lab4/line.h Normal file
View File

@ -0,0 +1,6 @@
struct info {
char c;
int length;
};
#define key ((key_t)(1243))
#define MSIZ sizeof(struct info)

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

BIN
CPE435/Lab4/process1 Executable file

Binary file not shown.

69
CPE435/Lab4/process1.c Normal file
View File

@ -0,0 +1,69 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include "info.h"
main(void)
{
int i, id;
struct info *ctrl;
int v1, v2;
int choice;
// create the shared memory segment
// .. id is the file identifier of the shared memory segment
// .. if the key is shared, any process can attach to this shared memory segment
// .. 0666 is the file permission for the shared memory segment
if ( (id = shmget( KEY,MSIZ,IPC_CREAT | 0666) ) < 0 )
{
//error…;
printf("error 1\n");
exit(1) ;
}
// attach a local pointer to the shared memory segment created
// .. exit if fails
if ( (ctrl = (struct info *) shmat( id, 0, 0)) <= (struct info *) (0) )
{
//error … ;
printf("error 2\n");
exit(1) ;
}
ctrl->flag=0;
ctrl->value1=0;
ctrl->value1=0;
ctrl->sum=0;
while(ctrl->flag!=-1)
{
//ctrl->flag=0;
// ask for numbers and set flag to 1
printf("Value 1: ");
scanf("%d",&v1);
printf("Value 2: ");
scanf("%d",&v2);
ctrl->value1 = v1;
ctrl->value2 = v2;
ctrl->flag = 1;
printf("Would you like to continue? Yes(1) or No(-1) : ");
scanf("%d",&choice);
if (choice == -1)
{
ctrl->flag = -1;
} else ctrl->flag=0;
}
//now detach the pointer from the shared memory
shmdt(ctrl);
//let us delete the shared memory
shmctl(id,IPC_RMID,NULL);
//job done
}

BIN
CPE435/Lab4/process2 Executable file

Binary file not shown.

46
CPE435/Lab4/process2.c Normal file
View File

@ -0,0 +1,46 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include "info.h"
int main()
{
int id;
struct info *ctrl;
id = shmget( KEY,MSIZ, 0 );
// get the id of the already created shared memory segment
// .. function to create is same as getting id of the already created shared memory segment
if ( id < 0 )
{
//error … ;
exit(1) ;
}
// attach a local pointer to the shared memory
ctrl = (struct info *) shmat( id, 0, 0);
if ( ctrl <= (struct info *) (0) )
{
//error … ;
exit(1) ;
}
while (ctrl->flag!=-1)
{
// if flag is 1, add the numbers from shared memory, set flag to 0
if (ctrl->flag==1)
{
ctrl->sum = ctrl->value1+ctrl->value2;
printf("Sum: %f + %f = %f\n", ctrl->value1, ctrl->value2, ctrl->sum);
ctrl->flag = 0;
}
}
//detach the pointer from the shared memory
// .. we do not need to delete here, can be done in either of the process
shmdt(ctrl);
exit(0);
}