FIFO命名管道
Once you have created a FIFO special file in this way, any process
can open it for reading or writing, in the same way as an ordinary file.However, it has to be open at both ends simultaneously before you can
proceed to do any input or output operations on it. Opening a FIFO for
reading normally blocks until some other process opens the same FIFO for
writing, and vice versa.
info mkfifo里的一段话.
一端open("fifo_file", O_RDONLY), 另一商必须对应的open("fifo_file", O_WRONLY),不然这个open会一直阻塞.
mqueue
posix默认mq_maxmsg是10,可通过echo "500" > /proc/sys/fs/mqueue/msg_max直接
修改,不然设置的attr超过msg_max值会出现Invalid argument.
mq_open的第一个参数必须是/xxxxx格式的,见man 7 mq_overview详解.
mutex lock
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define MAXITEMS 100000
#define MAXTHREADS 10
int items; // read-only by producer and consumer
struct {
pthread_mutex_t mutex;
int buff[MAXITEMS];
int nput;
int nval;
} shared = {PTHREAD_MUTEX_INITIALIZER};
void *producer(void *arg)
{
while (1)
{
pthread_mutex_lock(&shared.mutex);
if (shared.nput >= items)
{
pthread_mutex_unlock(&shared.mutex);
return NULL;
}
shared.buff[shared.nput] = shared.nval;
shared.nput++;
shared.nval++;
pthread_mutex_unlock(&shared.mutex);
*((int *)arg) += 1;
}
}
void *consumer(void *arg)
{
int i;
for (i = 0; i < items; i++)
{
if (shared.buff[i] != i)
printf("buff[%d] = %d\n", i, shared.buff[i]);
}
return 0;
}
int main(int argc, char *argv[])
{
printf("%s:%d:%d\n", shared.buff, shared.nput, shared.nval);
int i, threads, count[MAXTHREADS];
pthread_t tid_produce[MAXTHREADS], tid_consume;
items = MAXITEMS;
threads = MAXTHREADS;
// pthread_setconcurrency(threads);
// start producers
for (i = 0; i < threads; i++)
{
count[i] = 0;
pthread_create(&tid_produce[i], NULL, producer, &count[i]);
}
// wait for all producers terminate
for (i = 0; i < threads; i++)
{
pthread_join(tid_produce[i], NULL);
printf("count[%d] = %d\n", i, count[i]);
}
// start, then wait for the consumer thread
pthread_create(&tid_consume, NULL, consumer, NULL);
pthread_join(tid_consume, NULL);
return 0;
}
record lock
#include <fcntl.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
//#include <bits/fcntl.h>
#define LOCK_FILE "/tmp/rlock"
#define TEST_FILE "/tmp/test"
#define MAXLEN 1024
#define MAX 2000
void my_lock(int fd)
{
struct flock fk;
fk.l_type = F_WRLCK;
fk.l_whence = SEEK_SET;
fk.l_start = 0;
fk.l_len = 0;
fcntl(fd, F_SETLKW, &fk);
}
void my_unlock(int fd)
{
struct flock fk;
fk.l_type = F_UNLCK;
fk.l_whence = SEEK_SET;
fk.l_start = 0;
fk.l_len = 0;
fcntl(fd, F_SETLK, &fk);
}
int main(int argc, char *argv[])
{
int fd;
pid_t pid;
ssize_t n;
long seqno;
char line[MAXLEN];
int max;
long i;
FILE * ffd;
fd = open(LOCK_FILE, O_RDWR, 0633);
ffd = fopen(TEST_FILE, "a+");
pid = getpid();
// max = atoi(argv[1]);
for (i = 0; i < MAX; ++i)
{
my_lock(fd); // lock
lseek(fd, 0L, SEEK_SET); // rewind before read
n = read(fd, line, MAXLEN); //
line[n] = 0;
sscanf(line, "%ld\n", &seqno); //
fprintf(ffd, "%s: pid=%ld, seq#=%ld\n", argv[0], (long)pid, seqno);
seqno++;
snprintf(line, sizeof(line) + 1 , "%ld\n", seqno);
lseek(fd, 0L, SEEK_SET); // rewind before write
write(fd, line, strlen(line)+1);
my_unlock(fd); // unlock
}
fclose(ffd);
close(fd);
return 0;
}
write lock the daemon pid file make certain only one daemon running!
semaphore
int main(int argc, char *argv[])
{
sem_t *mutex;
pid_t pid;
int i;
int max = 1000;
/* */
mutex = sem_open("/semtest", O_CREAT | O_EXCL, 0666, 1);
sem_unlink("/semtest");
/* why create than unlink the sem, but can used it in next code */
pid = fork();
/* child */
if (pid == 0 )
对于上面的代码中
mutex = sem_open("/semtest", O_CREAT | O_EXCL, 0666, 1);
sem_unlink("/semtest");
这两行很是不解,怎么就直接给删掉了.在IPC书中shared map部分看到这样一段话
Create and initialize semaphore
la We create and initialize a semaphore that protects what we think is a shared variable
(the global count). Since this assumption is false, this semaphore is not really
needed. Notice that we remove the semaphore name from the system by calling
sem-unlink, but although this removes the pathname, it has no effect on the
semaphore that is already open. We do this so that the pathname is removed from the
filesystem even if the program aborts.