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 len;
- char pathname[] = "task2_w.c";
- key_t key;
- FILE *file;
- file = fopen(pathname, "r");
- fseek(file, 0L, SEEK_END);
- len = ftell(file);
- rewind(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), 0666|IPC_CREAT|IPC_EXCL)) < 0){
- if(errno != EEXIST){
- printf("Can\'t create shared memory\n");
- exit(-1);
- } else {
- 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);
- }
- *size = len;
- code = (char*)(size + 1);
- for (int i = 0; i < len; ++i) {
- code[i] = fgetc(file);
- }
- if(shmdt(size) < 0){
- printf("Can't detach shared memory\n");
- exit(-1);
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment