Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- #include <stdio.h>
- #include <errno.h>
- #include <stdlib.h>
- int main()
- {
- char *code;
- int shmid;
- int new = 1;
- char pathname[] = "task2_w.c";
- key_t key;
- FILE *file = fopen(pathname, "r");
- fseek(file, 0L, SEEK_END);
- int len = ftell(file);
- int* size;
- if((key = ftok(pathname,0)) < 0){
- printf("Can\'t generate key\n");
- exit(-1);
- }
- if((shmid = shmget(key, sizeof(int) + len * sizeof(char), 0)) < 0) {
- printf("Can\'t find shared memory\n");
- exit(-1);
- }
- if((size = (int *)shmat(shmid, NULL, 0)) == (int *)(-1)){
- printf("Can't attach shared memory\n");
- exit(-1);
- }
- len = *size;
- code = (char*)(size + 1);
- for (int i = 0; i < len; ++i) {
- putchar(code[i]);
- }
- if(shmdt(size) < 0){
- printf("Can't detach shared memory\n");
- exit(-1);
- }
- if (shmctl(shmid, IPC_RMID, NULL) < 0) {
- printf("Can't delete shared memory\n");
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment