forked from chyyuu/os_kernel_lab
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
026015e
commit fc1ceae
Showing
2 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#include <sys/types.h> | ||
#include <sys/ipc.h> | ||
#include <sys/shm.h> | ||
#include <stdio.h> | ||
#include <unistd.h> | ||
#include <string.h> | ||
|
||
#define SHMSZ 1024 | ||
|
||
main() | ||
{ | ||
int shmid; | ||
key_t key; | ||
char *shm, *s; | ||
|
||
/* | ||
* We need to get the segment named | ||
* "1234", created by the server. | ||
*/ | ||
key = 1234; | ||
|
||
/* | ||
* Locate the segment. | ||
*/ | ||
if ((shmid = shmget(key, SHMSZ, 0666)) < 0) { | ||
perror("shmget"); | ||
return 1; | ||
} | ||
|
||
/* | ||
* Now we attach the segment to our data space. | ||
*/ | ||
if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) { | ||
perror("shmat"); | ||
return 1; | ||
} | ||
|
||
/* | ||
* Zero out memory segment | ||
*/ | ||
memset(shm,0,SHMSZ); | ||
s = shm; | ||
|
||
/* | ||
* Client writes user input character to memory | ||
* for server to read. | ||
*/ | ||
for(;;){ | ||
char tmp = getchar(); | ||
// Eat the enter key | ||
getchar(); | ||
|
||
if(tmp == 'q'){ | ||
*shm = 'q'; | ||
break; | ||
} | ||
*shm = tmp; | ||
} | ||
|
||
if(shmdt(shm) != 0) | ||
fprintf(stderr, "Could not close memory segment.\n"); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#include <sys/types.h> | ||
#include <sys/ipc.h> | ||
#include <sys/shm.h> | ||
#include <stdio.h> | ||
#include <unistd.h> | ||
#include <string.h> | ||
|
||
#define SHMSZ 1024 | ||
|
||
main(int argc, char **argv) | ||
{ | ||
char c, tmp; | ||
int shmid; | ||
key_t key; | ||
char *shm, *s; | ||
|
||
/* | ||
* Shared memory segment at 1234 | ||
* "1234". | ||
*/ | ||
key = 1234; | ||
|
||
/* | ||
* Create the segment and set permissions. | ||
*/ | ||
if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) { | ||
perror("shmget"); | ||
return 1; | ||
} | ||
|
||
/* | ||
* Now we attach the segment to our data space. | ||
*/ | ||
if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) { | ||
perror("shmat"); | ||
return 1; | ||
} | ||
|
||
/* | ||
* Zero out memory segment | ||
*/ | ||
memset(shm,0,SHMSZ); | ||
s = shm; | ||
|
||
/* | ||
* Read user input from client code and tell | ||
* the user what was written. | ||
*/ | ||
while (*shm != 'q'){ | ||
sleep(1); | ||
if(tmp == *shm) | ||
continue; | ||
|
||
fprintf(stdout, "You pressed %c\n",*shm); | ||
tmp = *shm; | ||
} | ||
|
||
if(shmdt(shm) != 0) | ||
fprintf(stderr, "Could not close memory segment.\n"); | ||
|
||
return 0; | ||
} |