Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <errno.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- #define MAX_LEN 10000
- #define SELF_FILE "06-1-write.c"
- int main(int argc, char *argv[]) {
- char c;
- FILE *self;
- int i = 0;
- char *array;
- int shmid;
- int new = 1;
- char pathname[] = SELF_FILE;
- key_t key;
- if ((key = ftok(SELF_FILE, 0)) < 0) {
- printf("Can\'t generate key");
- exit(-1);
- }
- if ((shmid = shmget(key, MAX_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, MAX_LEN * sizeof(char), 0)) < 0) {
- printf("Can\'t find shared memory\n");
- exit(-1);
- }
- new = 0;
- }
- }
- if ((array = (char *)shmat(shmid, NULL, 0)) == (char *)(-1)) {
- printf("Can't attach shared memory\n");
- exit(-1);
- }
- self = fopen(SELF_FILE, "r");
- if (!self) {
- printf("Can't read file");
- exit(-1);
- }
- while ((c = getc(self)) != EOF) {
- array[i++] = c;
- }
- fclose(self);
- if (shmdt(array) < 0) {
- printf("Can't detach shared memory\n");
- exit(-1);
- }
- return 0;
- }
- #include <errno.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- #define MAX_LEN 10000
- #define SELF_FILE "06-1-write.c"
- int main(int argc, char *argv[]) {
- char c;
- int i;
- char *array;
- int shmid;
- int new = 1;
- char pathname[] = SELF_FILE;
- key_t key;
- if ((key = ftok(SELF_FILE, 0)) < 0) {
- printf("Can't generate key");
- exit(-1);
- }
- if ((shmid = shmget(key, MAX_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, MAX_LEN * sizeof(char), 0)) < 0) {
- printf("Can't find shared memory\n");
- exit(-1);
- }
- new = 0;
- }
- }
- if ((array = (char *)shmat(shmid, NULL, 0)) == (char *)(-1)) {
- printf("Can't attach shared memory\n");
- exit(-1);
- }
- for (i = 0; i < MAX_LEN; ++i) {
- putchar(array[i]);
- }
- if (shmdt(array) < 0) {
- printf("Can't detach shared memory\n");
- exit(-1);
- }
- shmctl(shmid, IPC_RMID, NULL);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement