mirror of
https://github.com/ada-dmitry/TMP_C712.git
synced 2026-09-24 09:10:16 +00:00
restart rep
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
#include <sys/types.h>
|
||||
#include <sys/ipc.h>
|
||||
#include <sys/shm.h>
|
||||
#include <sys/sem.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define errExit(msg) \
|
||||
do \
|
||||
{ \
|
||||
perror(msg); \
|
||||
exit(EXIT_FAILURE); \
|
||||
} while (0)
|
||||
|
||||
union semun
|
||||
{ /* Used in calls to semctl() */
|
||||
int val;
|
||||
struct semid_ds *buf;
|
||||
unsigned short *array;
|
||||
#if defined(__linux__)
|
||||
struct seminfo *__buf;
|
||||
#endif
|
||||
};
|
||||
|
||||
#define MEM_SIZE 4096
|
||||
@@ -0,0 +1,62 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/ipc.h>
|
||||
#include <sys/sem.h>
|
||||
#include <sys/shm.h>
|
||||
|
||||
#include "svshm_string.h"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int semid, shmid;
|
||||
char *addr;
|
||||
union semun arg, dummy;
|
||||
struct sembuf sop;
|
||||
|
||||
/* Create shared memory and semaphore set containing one
|
||||
semaphore. */
|
||||
|
||||
shmid = shmget(IPC_PRIVATE, MEM_SIZE, IPC_CREAT | 0600);
|
||||
if (shmid == -1)
|
||||
errExit("shmget");
|
||||
|
||||
semid = semget(IPC_PRIVATE, 1, IPC_CREAT | 0600);
|
||||
if (semid == -1)
|
||||
errExit("semget");
|
||||
|
||||
/* Attach shared memory into our address space. */
|
||||
|
||||
addr = shmat(shmid, NULL, SHM_RDONLY);
|
||||
if (addr == (void *)-1)
|
||||
errExit("shmat");
|
||||
|
||||
/* Initialize semaphore 0 in set with value 1. */
|
||||
|
||||
arg.val = 1;
|
||||
if (semctl(semid, 0, SETVAL, arg) == -1)
|
||||
errExit("semctl");
|
||||
|
||||
printf("shmid = %d; semid = %d\n", shmid, semid);
|
||||
|
||||
/* Wait for semaphore value to become 0. */
|
||||
|
||||
sop.sem_num = 0;
|
||||
sop.sem_op = 0;
|
||||
sop.sem_flg = 0;
|
||||
|
||||
if (semop(semid, &sop, 1) == -1)
|
||||
errExit("semop");
|
||||
|
||||
/* Print the string from shared memory. */
|
||||
|
||||
printf("%s\n", addr);
|
||||
|
||||
/* Remove shared memory and semaphore set. */
|
||||
|
||||
if (shmctl(shmid, IPC_RMID, NULL) == -1)
|
||||
errExit("shmctl");
|
||||
if (semctl(semid, 0, IPC_RMID, dummy) == -1)
|
||||
errExit("semctl");
|
||||
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/sem.h>
|
||||
#include <sys/shm.h>
|
||||
|
||||
#include "svshm_string.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int semid, shmid;
|
||||
char *addr;
|
||||
size_t len;
|
||||
struct sembuf sop;
|
||||
|
||||
if (argc != 4)
|
||||
{
|
||||
fprintf(stderr, "Usage: %s shmid semid string\n", argv[0]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
len = strlen(argv[3]) + 1; /* +1 to include trailing '\0' */
|
||||
if (len > MEM_SIZE)
|
||||
{
|
||||
fprintf(stderr, "String is too big!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
/* Get object IDs from command-line. */
|
||||
if((key = ftok("bin/sh", 'A')) = -1) errExit
|
||||
shmid = atoi(argv[1]);
|
||||
semid = atoi(argv[2]);
|
||||
|
||||
/* Attach shared memory into our address space and copy string
|
||||
(including trailing null byte) into memory. */
|
||||
|
||||
addr = shmat(shmid, NULL, 0);
|
||||
if (addr == (void *)-1)
|
||||
errExit("shmat");
|
||||
|
||||
memcpy(addr, argv[3], len);
|
||||
|
||||
/* Decrement semaphore to 0. */
|
||||
|
||||
sop.sem_num = 0;
|
||||
sop.sem_op = -1;
|
||||
sop.sem_flg = 0;
|
||||
|
||||
if (semop(semid, &sop, 1) == -1)
|
||||
errExit("semop");
|
||||
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
Reference in New Issue
Block a user