Created
June 12, 2020 04:23
-
-
Save redatawfik/95a37705b1a1d7206fa66b3e716e9376 to your computer and use it in GitHub Desktop.
Client implementation for POSIX client-server program
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
#include <sys/types.h> | |
#include <sys/ipc.h> | |
#include <sys/shm.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <time.h> | |
#define SIZE 2 | |
char getRand() { | |
int num = (rand() % 9) + 49; | |
return num; | |
} | |
int main() { | |
int shmid; | |
key_t key; | |
char *shm, *s; | |
key = 5678; | |
shmid = shmget(key, SIZE, 0666); | |
shm = shmat(shmid, NULL, 0); | |
srand(time(NULL)); | |
while (1) { | |
sleep(1); | |
puts("server:"); | |
puts(shm); | |
if (*shm == '7') { | |
exit(0); | |
} else { | |
s = shm; | |
*s = getRand(); | |
*s++; | |
*s = NULL; | |
} | |
puts("client:"); | |
puts(shm); | |
} | |
exit(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment