Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Semaphore 为什么信号量对象没有初始化?_Semaphore - Fatal编程技术网

Semaphore 为什么信号量对象没有初始化?

Semaphore 为什么信号量对象没有初始化?,semaphore,Semaphore,我正在学习使用信号量对象。但是我不能初始化它。 sem_init函数总是返回值-1雨或晴 返回值-1表示第一个参数不是有效指针,比如我的引用。 但我在代码中找不到打印小姐。我在OSX上用Xcode编译代码 #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <pthread.h> #include <semaphore.h> void * thread

我正在学习使用信号量对象。但是我不能初始化它。 sem_init函数总是返回值-1雨或晴

返回值-1表示第一个参数不是有效指针,比如我的引用。 但我在代码中找不到打印小姐。我在OSX上用Xcode编译代码

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>

void * thread_snd(void *arg);
void * thread_rcv(void* arg);

sem_t bin_sem;
int number = 0;

char thread1[] = "A thread";
char thread2[] = "B thread";
char thread3[] = "C thread";

int main(int argc, char** argv)
{
    pthread_t t1, t2 ,t3;
    void *thread_result;
    int state;

    state       =   sem_init(&bin_sem, 0, 0);
    if(state != 0)
    {
        puts("fail to initialize semaphore");
        exit(1);
    }

    pthread_create(&t1, NULL, thread_snd, &thread1);
    pthread_create(&t2, NULL, thread_rcv, &thread2);
    pthread_create(&t3, NULL, thread_rcv, &thread3);

    pthread_join(t1, &thread_result);
    pthread_join(t2, &thread_result);
    pthread_join(t3, &thread_result);

    printf("final number : %d \n", number);
    sem_destroy(&bin_sem);
    return 0;
}

void * thread_snd(void * arg)
{
    int i;
    for(i = 0 ; i < 4; i++)
    {
        while(number != 0)
            sleep(1);
        number++;
        printf("execution : %s, number : %d \n", (char*) arg, number);
        sem_post(&bin_sem);
    }
}

void * thread_rcv(void* arg)
{
    int i;
    for(i = 0 ; i < 2; i++)
    {
        sem_wait(&bin_sem);
        number--;
        printf("execution : %s number : %d \n", (char*)arg, number);
    }
}
#包括
#包括
#包括
#包括
#包括
void*螺纹(void*arg);
void*螺纹(void*arg);
sem_t bin_sem;
整数=0;
char thread1[]=“一个线程”;
char thread2[]=“B thread”;
char thread3[]=“C thread”;
int main(int argc,字符**argv)
{
pthread_t t1、t2、t3;
无效*线程结果;
int状态;
state=sem_init(&bin_sem,0,0);
如果(状态!=0)
{
puts(“未能初始化信号量”);
出口(1);
}
pthread_create(&t1,NULL,thread_snd,&thread1);
pthread_create(&t2,NULL,thread_rcv,&thread2);
pthread_create(&t3,NULL,thread_rcv,&thread3);
pthread_join(t1,&thread_结果);
pthread_join(t2,&thread_结果);
pthread_join(t3,&thread_结果);
printf(“最终编号:%d\n”,编号);
sem_销毁(和bin_sem);
返回0;
}
空*螺纹螺纹(空*参数)
{
int i;
对于(i=0;i<4;i++)
{
while(数字!=0)
睡眠(1);
数字++;
printf(“执行:%s,编号:%d\n”,(char*)arg,编号);
sem_post(和bin_sem);
}
}
void*线程\u rcv(void*arg)
{
int i;
对于(i=0;i<2;i++)
{
sem_wait(&bin_sem);
数字--;
printf(“执行:%s编号:%d\n”,(char*)参数,编号);
}
}
在Mac OS X(10.6.8)上没有
sem_init()
sem_destroy()

改用
sem\u open()
sem\u unlink()

/*

cat semaphore_test.c

source:
"Why semaphore object is not initialized?", 
https://stackoverflow.com/questions/13834367/why-semaphore-object-is-not-initialized

compiled on Mac OS X 10.6.8 with:
gcc -ansi -pedantic -std=gnu99 -Os -Wall -Wextra -Wshadow -Wpointer-arith -Wcast-qual  -Wstrict-prototypes \
    -Wmissing-prototypes -Wformat=2 -Wreturn-type -Wunreachable-code -finline  -l pthread -o semaphore_test semaphore_test.c

./semaphore_test

*/

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>

void * thread_snd(void *arg);
void * thread_rcv(void* arg);

//sem_t bin_sem;
static sem_t *bin_sem;
static const char *semname = "Semaphore";
static int number = 0;

char thread1[] = "A thread";
char thread2[] = "B thread";
char thread3[] = "C thread";

int main(void)
{
    pthread_t t1, t2 ,t3;
    void *thread_result;
    int state;

/*
    state  =  sem_init(&bin_sem, 0, 0);
    if(state != 0)
    {
        puts("fail to initialize semaphore");
        exit(1);
    }
*/

    bin_sem = sem_open(semname, O_CREAT, 0777, 0);
    if (bin_sem == SEM_FAILED)
    {
        fprintf(stderr, "%s\n", "ERROR creating semaphore semname");
        exit(EXIT_FAILURE);
    }

    pthread_create(&t1, NULL, thread_snd, &thread1);
    pthread_create(&t2, NULL, thread_rcv, &thread2);
    pthread_create(&t3, NULL, thread_rcv, &thread3);

    pthread_join(t1, &thread_result);
    pthread_join(t2, &thread_result);
    pthread_join(t3, &thread_result);

    printf("final number : %d \n", number);
    //sem_destroy(&bin_sem);
    sem_unlink(semname);
    return 0;
}

void * thread_snd(void * arg)
{
    int i;
    for(i = 0 ; i < 4; i++)
    {
        while(number != 0)
            sleep(1);
        number++;
        printf("snd execution : %s, number : %d \n", (char*) arg, number);
        //sem_post(&bin_sem);
        sem_post(bin_sem);
    }
}

void * thread_rcv(void* arg)
{
    int i;
    for(i = 0 ; i < 2; i++)
    {
        //sem_wait(&bin_sem);
        sem_wait(bin_sem);
        number--;
        printf("rcv execution : %s number : %d \n", (char*)arg, number);
    }
}
/*
cat信号灯测试.c
资料来源:
“为什么信号量对象未初始化?”,
https://stackoverflow.com/questions/13834367/why-semaphore-object-is-not-initialized
在Mac OS X 10.6.8上编译,具有:
gcc-ansi-pedantic-std=gnu99-Os-Wall-Wextra-Wshadow-Wpointer-arith-Wcast-qual-Wstrict原型\
-Wmissing prototype-Wformat=2-Wreturn type-Wunreachable code-finline-l pthread-o semaphore\u test semaphore\u test.c
/信号量测试
*/
#包括
#包括
#包括
#包括
#包括
void*螺纹(void*arg);
void*螺纹(void*arg);
//sem_t bin_sem;
静态扫描电镜*bin扫描电镜;
静态常量char*semname=“信号量”;
静态整数=0;
char thread1[]=“一个线程”;
char thread2[]=“B thread”;
char thread3[]=“C thread”;
内部主(空)
{
pthread_t t1、t2、t3;
无效*线程结果;
int状态;
/*
state=sem_init(&bin_sem,0,0);
如果(状态!=0)
{
puts(“未能初始化信号量”);
出口(1);
}
*/
bin_sem=sem_open(semname,O_CREAT,0777,0);
如果(bin_sem==sem_失败)
{
fprintf(stderr,“%s\n”,“创建信号量semname时出错”);
退出(退出失败);
}
pthread_create(&t1,NULL,thread_snd,&thread1);
pthread_create(&t2,NULL,thread_rcv,&thread2);
pthread_create(&t3,NULL,thread_rcv,&thread3);
pthread_join(t1,&thread_结果);
pthread_join(t2,&thread_结果);
pthread_join(t3,&thread_结果);
printf(“最终编号:%d\n”,编号);
//sem_销毁(和bin_sem);
sem_unlink(semname);
返回0;
}
空*螺纹螺纹(空*参数)
{
int i;
对于(i=0;i<4;i++)
{
while(数字!=0)
睡眠(1);
数字++;
printf(“snd执行:%s,编号:%d\n”,(char*)arg,编号);
//sem_post(和bin_sem);
sem_post(bin_sem);
}
}
void*线程\u rcv(void*arg)
{
int i;
对于(i=0;i<2;i++)
{
//sem_wait(&bin_sem);
sem_wait(bin_sem);
数字--;
printf(“rcv执行:%s编号:%d\n”,(char*)arg,编号);
}
}
另见: