3つのexecv( "./ test"、execv_str)を並行して実行しようとしています。 execv()が正常に完了すると、成功メッセージを出力する必要があります。Cで並列にfork()とexec()を実行
しかし、今、私は次のようになります:
[email protected]:~/Desktop/$./test -p
SUCCESS
SUCCESS
SUCCESS
[email protected]:~/Desktop/$ TESTING
TESTING
TESTING
を期待される結果は次のようになります。
[email protected]:~/Desktop/$./test -p
TESTING
SUCCESS
TESTING
SUCCESS
TESTING
SUCCESS
[email protected]:~/Desktop/$
ここでは、コードです。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int fork_execv()
{
int status;
pid_t pid;
pid = fork();
/* Handling Child Process */
if(pid == 0){
char* execv_str[] = {"./test", NULL};
if (execv("./test",execv_str) < 0){
status = -1;
perror("ERROR\n");
}
}
/* Handling Child Process Failure */
else if(pid < 0){
status = -1;
perror("ERROR\n");
}
return status;
}
int main(int argc, char *argv[]){
if (argc == 1){
sleep(5);
printf("TESTING\n");
}
else{
int i;
for(i = 0; i < 3; ++i){
if (fork_execv() != -1){
printf("SUCCESS\n");
}
}
}
}
コードを変更して正常に動作させるにはどうすればよいですか?
Cはマルチスレッドをサポートしていません –
@DeepeshChoudhary - この質問にはスレッドは含まれません。 (そして、Cは実際にスレッドをサポートしていることに注意してください。) –
@ Oliver Charlesworth本当に?方法を教えてください(またはリンクを共有してください)。私は長い間、それをc言語で使いたいと思っていました。 –