使用函数sem_init时发生错误

发布于 2025-02-07 06:40:33 字数 1628 浏览 1 评论 0原文

我正在使用Linux学习信号,trnna测试函数sem_init。代码似乎很简单,但错误是出乎意料的,不知道为什么。这是代码,错误在代码以下。

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

#define NITER 1000000

int cnt = 0;
sem_t mutex;
sem_init(&mutex,0,1);

void * Count(void * a)
{
    int i, tmp;
    for(i = 0; i < NITER; i++)
    {
        sem_wait(&mutex);
        tmp = cnt;      /* copy the global cnt locally */
        tmp = tmp+1;    /* increment the local copy */
        cnt = tmp;      /* store the local value into the global cnt */ 
        sem_post(&mutex);
    }
}

int main(int argc, char * argv[])
{
    pthread_t tid1, tid2;

    if(pthread_create(&tid1, NULL, Count, NULL))
    {
      printf("\n ERROR creating thread 1");
      exit(1);
    }

    if(pthread_create(&tid2, NULL, Count, NULL))
    {
      printf("\n ERROR creating thread 2");
      exit(1);
    }

    if(pthread_join(tid1, NULL))    /* wait for the thread 1 to finish */
    {
      printf("\n ERROR joining thread");
      exit(1);
    }

    if(pthread_join(tid2, NULL))        /* wait for the thread 2 to finish */
    {
      printf("\n ERROR joining thread");
      exit(1);
    }

    if (cnt < 2 * NITER) 
        printf("\n BOOM! cnt is [%d], should be %d\n", cnt, 2*NITER);
    else
        printf("\n OK! cnt is [%d]\n", cnt);
  
    pthread_exit(NULL);
}

我的英语很糟糕,如果有什么难以理解的话,请告诉我并尽快回复您!

I'm using Linux to learn semaphore, trnna test the function sem_init. The code seems simple but the error is unexpected, don't know why. Here's the code and the errors is below the code.

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

#define NITER 1000000

int cnt = 0;
sem_t mutex;
sem_init(&mutex,0,1);

void * Count(void * a)
{
    int i, tmp;
    for(i = 0; i < NITER; i++)
    {
        sem_wait(&mutex);
        tmp = cnt;      /* copy the global cnt locally */
        tmp = tmp+1;    /* increment the local copy */
        cnt = tmp;      /* store the local value into the global cnt */ 
        sem_post(&mutex);
    }
}

int main(int argc, char * argv[])
{
    pthread_t tid1, tid2;

    if(pthread_create(&tid1, NULL, Count, NULL))
    {
      printf("\n ERROR creating thread 1");
      exit(1);
    }

    if(pthread_create(&tid2, NULL, Count, NULL))
    {
      printf("\n ERROR creating thread 2");
      exit(1);
    }

    if(pthread_join(tid1, NULL))    /* wait for the thread 1 to finish */
    {
      printf("\n ERROR joining thread");
      exit(1);
    }

    if(pthread_join(tid2, NULL))        /* wait for the thread 2 to finish */
    {
      printf("\n ERROR joining thread");
      exit(1);
    }

    if (cnt < 2 * NITER) 
        printf("\n BOOM! cnt is [%d], should be %d\n", cnt, 2*NITER);
    else
        printf("\n OK! cnt is [%d]\n", cnt);
  
    pthread_exit(NULL);
}

my english is bad, if anything is hard to understand please tell me and ill respond you as soon as possible!thx
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遮云壑 2025-02-14 06:40:33

您可以在主函数和其他函数之外声明变量,这意味着该变量是全局的。

但是要调用函数sem_init(&amp; mutex,0,1);应该从主或另一个函数中进行。就您而言,您必须在主要的情况下进行。

将SEM_INIT移入主内置之后

nabil@DESKTOP-8ECTID4:~/stackoverflow$ gcc bad.c -o bad -lpthread
nabil@DESKTOP-8ECTID4:~/stackoverflow$

You can declare variables outside the main and the other function which mean that the variable is global .

But to call the function sem_init(&mutex,0,1); you should do it from the main or from another function. In your case , you have to do it in the main.

After moving the sem_init inside the main it compile

nabil@DESKTOP-8ECTID4:~/stackoverflow$ gcc bad.c -o bad -lpthread
nabil@DESKTOP-8ECTID4:~/stackoverflow$
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文