[RAND_PROC], [STOREPTR], [Asynchronous terminal IO]
RAND_PROC:
Obtain the procid of this process;
Initialize the random seed;
Initialization;
Lock COUNT's lock;
if segid.dat does not exist then
create and initialize the segment;
write 1 to COUNT
create the segid.dat file and write the segment id to it
else
read the segment id from segid.dat
increment COUNT
UnLock COUNT's lock;
Attach to the segmentid and set STOREPTR to value returned by Attach;
[All access to STORE is done through STOREPTR]
Display Menu;
loop
If there is input from terminal then
Carry out the user request and, if necessary,
Display Menu;
Select at random STORE_READ or STORE_UPDATE
and execute the corresponding
function [in the function you will use locks, sleep, etc.]
and log the call and result;
wait some random time;
end loop;
Lock COUNT's lock;
Detach from shared segment
Decrement COUNT
if COUNT becomes 0 then
Delete the segid.dat file
Delete the shared segment
Unlock COUNT's lock;
Conclusion;
#define STORESIZE ..
#define STOREIDSIZE ..
#define STORELEMSIZE ..
typedef char storeid[STOREIDSIZE];
typedef char storeelem[STORELEMSIZE];
typedef struct {
storeid id;
storeelem elem;} storepair;
typedef storepair storetype[STORESIZE];
storetype * storeptr;
void storeinit();
int main () {
storeptr = (storetype *) malloc(sizeof(storetype));
storeinit();
printf ("id of second entry: %s\n", storeptr[1]->id);
exit(0);
}
void storeinit(){
strcpy(storeptr[0]->id, "......");
...........
}
/* asynctty.c -- Uses non blocking reads from the terminal.
*/
#include <stdio.h>
#include <fcntl.h>
#define LINESIZE 128
void menu(void);
main()
{
char line[LINESIZE];
int size;
int fd;
menu();
if ((fd = open("/dev/tty", O_RDONLY | O_NONBLOCK, "r")) < 0) {
perror("open");
exit(1);
}
while(TRUE) {
sleep(2);
size = read(fd, line, 1);
if (size > 0) {
if (line[0] == '1') {
/*Take out all that was printed */
while ((size = read(fd,line,1)) > 0);
printf("You have asked for statistics\n");
menu();
} else {
printf("You have asked to terminate\n");
break;
}
} else
printf("Spinning\n");
}
printf("\nGoodbye\n");
}
void menu(void) {
printf("1. Print Statistics\n");
printf("2. Terminate\n");
printf("Enter Choice [1|2] > \n");
}
ingargiola@cis.temple.edu