简体   繁体   中英

Does system() do a call like sem_post inside?

I think my code wouldn't print the text

oh why come here!\\n

but it does.

Is there something 'wrong' with 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


I compiled it with gcc 4.6.3, kernel 3.2.0, it ran as I want also. So I think it is because of gcc 4.1.2 or kernel 2.6.18.

The system() call has nothing to do with it. My psychic powers tell me that sem_wait is failing with an error code instead of waiting—check the return value. 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").

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. Currently, your code invokes undefined behavior by failing to return values from your fun0 and fun1 functions, which -Wall would warn you about. This kind of error may not cause any obvious problems on x86, but on other architectures such as IA64, uninitialized garbage can be deadly .

Problem of your code is with sem_wait(), from sem_wait man page, it says:

"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."

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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