简体   繁体   English

system()是否在内部执行类似sem_post的调用?

[英]Does system() do a call like sem_post inside?

I think my code wouldn't print the text 我认为我的代码不会打印文本

oh why come here!\\n 哦,为什么要来这里!\\ n

but it does. 但确实如此。

Is there something 'wrong' with system() ? system()是否有“问题”? Because, when I removed it, the code ran as I wanted, halting up. 因为,当我删除它时,代码按我的意愿运行,停止了。

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

pthread_t id0, id1;
sem_t sp; 

void *fun0(void *) {
    // When erasing the following line "system("");",
    // it block up, and doesn't print "oh why come here!\n".
    // But with it, it print the text!
    system("");
    return NULL;
}

void *fun1(void *) {
    sem_wait(&sp);
    fprintf(stderr, "oh why come here!\n");
    return NULL;
}
int main() {
    sem_init(&sp, 0, 0); 
    pthread_create(&id0, 0, fun0, NULL);
    pthread_create(&id1, 0, fun1, NULL);
    void *stat0, *stat1;
    pthread_join(id0, &stat0);
    pthread_join(id1, &stat1);
    return 0;
}

Compiler: gcc 4.1.2 Linux kernel: 2.6.18 编译器:gcc 4.1.2 Linux内核:2.6.18


I compiled it with gcc 4.6.3, kernel 3.2.0, it ran as I want also. 我使用gcc 4.6.3,内核3.2.0对其进行了编译,并且还可以根据需要运行。 So I think it is because of gcc 4.1.2 or kernel 2.6.18. 所以我认为这是因为gcc 4.1.2或内核2.6.18。

The system() call has nothing to do with it. system()调用与此无关。 My psychic powers tell me that sem_wait is failing with an error code instead of waiting—check the return value. 我的灵性告诉我sem_wait失败,并显示错误代码,而不是等待—检查返回值。 For example, I can reproduce your results on Mac OS X, because on Mac OS X, sem_init() always fails with ENOSYS ("Function not implemented"), which causes the call to sem_wait to then fail with EBADF ("Bad file descriptor"). 例如,我可以在Mac OS X上重现您的结果,因为在Mac OS X上, sem_init()始终因ENOSYS (“未实现功能”)而失败,这导致对sem_wait的调用随后因EBADF (“错误的文件描述符”而失败) “)。

If you add some error checking, you'll see where things go awry: 如果添加一些错误检查,您会发现问题出在哪里:

if(sem_init(&sp, 0, 0) < 0)
    fprintf(stderr, "sem_init failed: %s\n", strerror(errno));
...
if(sem_wait(&sp) < 0)
    fprintf(stderr, "sem_wait failed: %s\n", strerror(errno));

You should also crank up the warning level on your compiler—I definitely recommend using -Wall , and -Wextra -pedantic if you want to catch a lot more possible problems. 您还应该提高编译器的警告级别-如果您想捕获更多可能的问题,我绝对建议使用-Wall-Wextra -pedantic Currently, your code invokes undefined behavior by failing to return values from your fun0 and fun1 functions, which -Wall would warn you about. 当前,代码无法通过未从fun0fun1函数返回值来调用未定义的行为-Wall会警告您。 This kind of error may not cause any obvious problems on x86, but on other architectures such as IA64, uninitialized garbage can be deadly . 这种错误可能不会在x86上引起任何明显的问题,但是在其他架构(例如IA64)上, 未初始化的垃圾可能是致命的

Problem of your code is with sem_wait(), from sem_wait man page, it says: 您的代码出现问题的原因是sem_wait手册页中的sem_wait(),它说:

"sem_wait() decrements (locks) the semaphore pointed to by sem. If the semaphore's value is greater than zero, then the decrement proceeds, and the function returns, immediately. If the semaphore currently has the value zero, then the call blocks until either it becomes possible to perform the decrement (ie, the semaphore value rises above zero), or a signal handler interrupts the call." “ sem_wait()递减(锁定)sem指向的信号量。如果该信号量的值大于零,则该减量继续进行,该函数立即返回。如果该信号量当前的值为零,则调用将阻塞直到或者可以执行减量操作(即,信号量值增加到零以上),或者信号处理程序中断调用。”

On your code you've initialized with sp with 0, when sem_wait() decrements, it blocks and never returns since there's no other thread increments sp variable. 在代码上,您已使用sp初始化为0,当sem_wait()减1时,它将阻塞并且永远不会返回,因为没有其他线程增量sp变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM