added sock20

This commit is contained in:
ada-dmitry
2023-10-20 15:00:30 +03:00
parent e19c8aa5c5
commit 29db3dbe27
7 changed files with 584 additions and 142 deletions
+48 -46
View File
@@ -2,59 +2,61 @@
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <unistd.h>
#include <sys/shm.h>
static void
usage(const char *pname)
#include "svshm_string.h"
int main(void)
{
fprintf(stderr, "Usage: %s [-cx] pathname proj-id num-sems\n",
pname);
fprintf(stderr, " -c Use IPC_CREAT flag\n");
fprintf(stderr, " -x Use IPC_EXCL flag\n");
exit(EXIT_FAILURE);
}
int semid, shmid;
char *addr;
union semun arg, dummy;
struct sembuf sop;
int main(int argc, char *argv[])
{
int semid, nsems, flags, opt;
key_t key;
/* Create shared memory and semaphore set containing one
semaphore. */
flags = 0;
while ((opt = getopt(argc, argv, "cx")) != -1)
{
switch (opt)
{
case 'c':
flags |= IPC_CREAT;
break;
case 'x':
flags |= IPC_EXCL;
break;
default:
usage(argv[0]);
}
}
shmid = shmget(IPC_PRIVATE, MEM_SIZE, IPC_CREAT | 0600);
if (shmid == -1)
errExit("shmget");
if (argc != optind + 3)
usage(argv[0]);
key = ftok(argv[optind], argv[optind + 1][0]);
if (key == -1)
{
perror("ftok");
exit(EXIT_FAILURE);
}
nsems = atoi(argv[optind + 2]);
semid = semget(key, nsems, flags | 0600);
semid = semget(IPC_PRIVATE, 1, IPC_CREAT | 0600);
if (semid == -1)
{
perror("semget");
exit(EXIT_FAILURE);
}
errExit("semget");
printf("ID = %d\n", semid);
/* 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);
}