- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试用信号量、fork 和共享内存做一个程序
父节点假设创建共享内存并将数字插入缓冲区 print done然后 child 将从共享内存中读取并打印数字的总和程序运行但只有父打印完成但 child 不做任何事情,
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <stdio.h>
#include <netinet/in.h>
#include <netdb.h>
#include<netinet/in.h>
#include<netdb.h>
#include<sys/wait.h>
#include<unistd.h>
#include <sys/shm.h>
char buffer[10];
char *data;
int commNo =0;
key_t key;
int shmid;
char *shrmem;
int mode;
void error(char *msg)
{
perror(msg);
exit(0);
}
int rem[10];
key_t key; /* key to pass to semget() */
key_t key1; /* key to pass to semget() */
key_t key2; /* key to pass to semget() */
key_t key3; /* key to pass to semget() */
int semflg; /* semflg to pass tosemget() */
int nsems; /* nsems to pass to semget() */
int semid; /* return value from semget() */
#define MAX_COUNT 200
#define N 100 /* shared buffer of size 100 */
void ChildProcess(void); /* child process prototype */
void ParentProcess(void); /* parent process prototype */
//things for ftok
char *path = "/tmp";
int id = 'S';
//create sembuf
//struct sembuf{
// ushort sem_num; /* semaphore index in array */
// short sem_op; /* semaphore operation */
// short sem_flg; /* operation flags */
//};
int locksem = 1; /* binary semaphore for locking-unlocking: initiall unlocked : 1 */
int emptysem = 100; /* counting semaphore for empty slots */
int fullsem = 0; /* counting semaphore for filled slots*/
struct sembuf oplock;// = (struct sembuf *) malloc(2*sizeof(struct sembuf));
//static struct sembuf oplock;
//oplock.sem_num = 0;
//oplock.sem_op = -1;
//oplock.sem_flg = SEM_UNDO;
struct sembuf opunlock; // = (struct sembuf *) malloc(2*sizeof(struct sembuf));
//static struct sembuf opunlock;
//opunlock.sem_num = 0;
//opunlock.sem_op = 1;
//opunlock.sem_flg = SEM_UNDO;
void creatmemory(){
/* make the key: */
if ((key = ftok("/tmp",1)) == -1) {
perror("ftok");
//clientnumber
exit(1);
}
/* connect to (and possibly create) the segment: */
if ((shmid = shmget(key, 15, 0644| IPC_CREAT)) == -1) {
perror("shmget");
exit(1);
}
/* attach to the segment to get a pointer to it: */
data = shmat(shmid, (void *)0, 0);
if (data == (char *)(-1)) {
perror("shmat");
exit(1);
}
}
int main(int argc, char *argv[]){
pid_t pid;
nsems = 1;
//static struct sembuf oplock;
oplock.sem_num = 0;
oplock.sem_op = -1;
oplock.sem_flg = SEM_UNDO;
//static struct sembuf opunlock;
opunlock.sem_num = 0;
opunlock.sem_op = 1;
opunlock.sem_flg = SEM_UNDO;
//to create new one
semflg = IPC_CREAT;
/* Get unique key for semaphore. */
if ((key1 = ftok(path,1)) == (key_t) -1) {
perror("IPC error: ftok"); exit(1);
}
/* Get unique key for semaphore. */
if ((key2 = ftok(path,2)) == (key_t) -1) {
perror("IPC error: ftok"); exit(1);
}
/* Get unique key for semaphore. */
if ((key3 = ftok(path,3)) == (key_t) -1) {
perror("IPC error: ftok"); exit(1);
}
pid = fork();
if (pid == 0) {
//***************************************************************************************************************
//
int n =0;
while(n < 3){
locksem = semget(key1, nsems, semflg); /* open-create locksem*/
emptysem = semget(key2, nsems, semflg);
fullsem = semget(key3, nsems, semflg);
semop(fullsem,&oplock,1); /* check filled slots */
semop(locksem,&oplock,1); /* lock shared buffer*/
// itx= remove_item();
/* remove an item from buffer */
int i;
for (i =0; i<10; i++){
rem[i] = data[i];}
bzero(buffer,10);
bzero(data,strlen(data));
/* detach from the segment: */
if (shmdt(data) == -1) {
perror("shmdt");
exit(1);
}
semop(locksem,&opunlock,1); /* unlock the buffer */
semop(emptysem,&opunlock,1); /* increment emptysem*/
//consume_item(itx);
int sum = 0;
for(i =0; i<10; i++){
sum += rem[i];}
printf("the sum is:%d \n", sum);
n++;}
}
//consumer();
else
{
creatmemory();
int n =0;
while( n < 3) {
locksem = semget(key1, nsems, semflg); /* open-create locksem*/
emptysem = semget(key2, nsems, semflg);
fullsem = semget(key3, nsems, semflg);
bzero(buffer,10);
int i;
for(i =0; i<10; i++)
buffer[i] = i;
semop(emptysem,&oplock,1); /* check empty slots */
semop(locksem,&oplock,1); /* lock shared buffer*/
/* insert item into the buffer */
strncpy(data, buffer, strlen(buffer));
printf("done \n");
semop(locksem,&opunlock,1); /* unlock the buffer */
semop(fullsem,&opunlock,1); /* increment fullsem*/
sleep(10);
n++;}
}
// producer();
}
最佳答案
这是实际编译和链接干净的代码版本:
#define _XOPEN_SOURCE (1) // needed by sys/ipc.h
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <stdio.h>
#include <stdlib.h> // exit()
#include <string.h>
#include <strings.h> // bzero())
#include <netinet/in.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/shm.h>
char buffer[10];
char *data;
int commNo =0;
key_t key;
int shmid;
char *shrmem;
int mode;
void error(char *);
int rem[10];
key_t key; /* key to pass to semget() */
key_t key1; /* key to pass to semget() */
key_t key2; /* key to pass to semget() */
key_t key3; /* key to pass to semget() */
int semflg; /* semflg to pass tosemget() */
int nsems; /* nsems to pass to semget() */
int semid; /* return value from semget() */
#define MAX_COUNT 200
#define N 100 /* shared buffer of size 100 */
void creatmemory( void );
//things for ftok
char *path = "/tmp";
int id = 'S';
//create sembuf
//struct sembuf{
// ushort sem_num; /* semaphore index in array */
// short sem_op; /* semaphore operation */
// short sem_flg; /* operation flags */
//};
int locksem = 1; /* binary semaphore for locking-unlocking: initiall unlocked : 1 */
int emptysem = 100; /* counting semaphore for empty slots */
int fullsem = 0; /* counting semaphore for filled slots*/
struct sembuf oplock;// = (struct sembuf *) malloc(2*sizeof(struct sembuf));
//static struct sembuf oplock;
//oplock.sem_num = 0;
//oplock.sem_op = -1;
//oplock.sem_flg = SEM_UNDO;
struct sembuf opunlock; // = (struct sembuf *) malloc(2*sizeof(struct sembuf));
//static struct sembuf opunlock;
//opunlock.sem_num = 0;
//opunlock.sem_op = 1;
//opunlock.sem_flg = SEM_UNDO;
int main()
{
pid_t pid;
nsems = 1;
//static struct sembuf oplock;
oplock.sem_num = 0;
oplock.sem_op = -1;
oplock.sem_flg = SEM_UNDO;
//static struct sembuf opunlock;
opunlock.sem_num = 0;
opunlock.sem_op = 1;
opunlock.sem_flg = SEM_UNDO;
//to create new one
semflg = IPC_CREAT;
/* Get unique key for semaphore. */
if ((key1 = ftok(path,1)) == (key_t) -1) {
perror("IPC error: ftok"); exit(1);
}
/* Get unique key for semaphore. */
if ((key2 = ftok(path,2)) == (key_t) -1) {
perror("IPC error: ftok"); exit(1);
}
/* Get unique key for semaphore. */
if ((key3 = ftok(path,3)) == (key_t) -1) {
perror("IPC error: ftok"); exit(1);
}
if( 0 == (pid = fork() ) )
{ // then, child process
int n =0;
for(;n<3;n++)
{
locksem = semget(key1, nsems, semflg); /* open-create locksem*/
emptysem = semget(key2, nsems, semflg);
fullsem = semget(key3, nsems, semflg);
semop(fullsem,&oplock,1); /* check filled slots */
semop(locksem,&oplock,1); /* lock shared buffer*/
// itx= remove_item();
/* remove an item from buffer */
int i = 0;
for (;i<10; i++)
{
rem[i] = data[i];
} // end for
bzero(buffer,10);
bzero(data,strlen(data));
/* detach from the segment: */
if (shmdt(data) == -1)
{
error( "shmdt" );
} // error() does not return
// implied else, shmdt successful
semop(locksem,&opunlock,1); /* unlock the buffer */
semop(emptysem,&opunlock,1); /* increment emptysem*/
//consume_item(itx);
int sum = 0;
for(i =0; i<10; i++)
{
sum += rem[i];
} // end for
printf("the sum is:%d \n", sum);
} // end while
}
else
{ // else parent process
creatmemory();
int n =0;
for(;n<3;n++)
{
locksem = semget(key1, nsems, semflg); /* open-create locksem*/
emptysem = semget(key2, nsems, semflg);
fullsem = semget(key3, nsems, semflg);
bzero(buffer,10);
int i;
for(i =0; i<10; i++)
{
buffer[i] = i;
} // end for
semop(emptysem,&oplock,1); /* check empty slots */
semop(locksem,&oplock,1); /* lock shared buffer*/
/* insert item into the buffer */
strncpy(data, buffer, strlen(buffer));
printf("done \n");
semop(locksem,&opunlock,1); /* unlock the buffer */
semop(fullsem,&opunlock,1); /* increment fullsem*/
sleep(10);
} // end for
} // end if
return(0);
} // end function: main
void creatmemory()
{
/* make the key: */
if ((key = ftok("/tmp",1)) == -1)
{
error( "ftok" ); // error does not return
}
// implied else, ftok successful
/* connect to (and possibly create) the segment: */
if ((shmid = shmget(key, 15, 0644| IPC_CREAT)) == -1)
{
error( "shmget" ); // error does not return
}
// implied else, shmget successful
/* attach to the segment to get a pointer to it: */
data = shmat(shmid, (void *)0, 0);
if (data == (char *)(-1))
{
error( "shmat" ); // error does not return
}
// implied else, shmat successful
} // end function: creatmemory
void error(char *msg)
{
perror(msg);
exit(0);
} // end function: error
我建议使用互斥量而不是信号量来保护关键代码/数据。
顺便说一句:除非被摧毁,否则信号量将永远存在。
在 Linux 中,它们成为/proc 目录树的一部分。
关于c - C 信号量中的 POSIX 具有 fork 和共享内存,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27760792/
有3个 repo : 有 OpenAI Baselines 存储库:https://github.com/openai/baselines . 我有它的 fork :https://github.co
我试图了解在调用 fork() 后复制文件描述符的含义及其对争用的可能影响。 在“Linux 编程接口(interface)”24.2.1 (p517) 中: When a fork() is per
我对 systemd 如何跟踪主进程存在后仍然存在的主进程的子进程感兴趣? 最佳答案 Systemd 为此使用了 cgroups。在启动任何使用服务文件定义的可执行文件之前,systemd 会创建一个
这是输出--- 家长:我的pid是4525 parent :我 parent 的 pid 是 3350 parent 开始- 4525 3350 fork 前 fork 前 child 4526 45
我之前 fork 了 jockm/vert.x 并向他发送了拉取请求。现在我想 fork vert-x/vert.x (jockm/vert.x 的上游)并向他们发送不同的拉取请求。但是,当我单击“F
我想控制从 Perl 脚本派生的进程的名称 a。理想情况下它会像这样: ./forker.pl | ... | | fork("forked.pl"); |\ | \ | `--------\ |
我知道 fork() 在更高级别上做什么。我想知道的是这个—— 一旦有 fork 调用,trap 指令就会跟随并且控制跳转以执行 fork “处理程序”。现在,这个创建子进程的处理程序如何通过创建另一
我正在研究操作系统测验,但我不知道输出什么 if(fork()) fork() 会产生。有人可以解释吗? 我不明白这一行: if(fork()) 编辑: 我所说的“输出”是指如果执行此代码,将
这个问题在这里已经有了答案: Why does this program print "forked!" 4 times? (6 个答案) 关闭 3 年前。 在 C 中,fork() 函数将为父进程
有什么方法可以区分程序中不同 fork() 函数创建的子进程。 global variable i; SIGCHLD handler function() { i--; } handle() {
我正在重新开发一个系统,该系统将通过 http 向多个供应商之一发送消息。原来是perl脚本,重新开发很可能也会用perl。 在旧系统中,同时运行多个 perl 脚本,每个供应商运行 5 个。当一条消
Git 的新手,仍然有点困惑。我在 github 上 fork 了一个项目,想将项目所有者最近对原始项目所做的一些更改引入/merge 到我的 fork 中。这可能吗?该项目是只读的,但基本上,我想让
根据维基百科(可能是错误的) When a fork() system call is issued, a copy of all the pages corresponding to the par
我需要帮助了解如何在 Go 中妖魔化进程。 package main import ( "fmt" "os" ) func start() { var procAttr os.Pro
我已经执行了这段代码。我知道消息的顺序是任意顺序的(因为我明确没有使用信号量)我的程序流程如何?为什么? 父级被执行,因此“baz”被打印一次。有人可以解释为什么不打印“bar”吗?为什么我得到“fo
这个问题已经有答案了: Why does this program print "forked!" 4 times? (6 个回答) 已关闭 5 年前。 我对 fork 进程有疑问。我有一个代码是 i
我在弄清楚如何使用在不同进程之间创建的列表时遇到了麻烦。我所拥有的是: FileList.h - 我创建的列表 #include "Node.h" typedef struct FileList {
好吧,所以我一直在 stackoverflow 上查找这个问题,并且肯定在谷歌上搜索了半个小时,但我得到的答案似乎与我真正想做的事情几乎没有任何关系,希望有人能提供帮助我在这方面,代码如下: int
我正在尝试了解 fork-join 的工作原理。 维基百科有以下合并排序示例,其中左半部分被 fork ,右半部分由当前线程处理。 mergesort(A, lo, hi): if lo t
下面的代码输出了一系列的过程,它到底做了什么: _PARENT_ / \ /
我是一名优秀的程序员,十分优秀!