结束gcc C Linux中的第一个成功的线程 [英] Ending the first successful thread in gcc C Linux

查看:133
本文介绍了结束gcc C Linux中的第一个成功的线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一位经验丰富的C程序员,从未使用线程或并行。我一直在阅读它,但我没有看到我想要的例子。



我在Mac和Linux上使用gcc C编译器。我想用一个新的程序X2替换我的系统中的一个重要的程序X,该程序将启动两种方法,即两个线程在两台不同的处理器上运行,只要机器有多个CPU(这些日子大部分都是这样)。



这两个方法可能共享一些全局变量,但它们不会写入除自己堆栈之外的任何内存位置。他们每个人都会在系统中调用其他许多程序。我没有设想任何其他的并行处理。



一旦任何线程完成,就是这样!这就是答案。 X2应该立即杀死其他线程,并将答案返回给称为X2的人。

可能我很天真,但我认为这是众所周知的使用线程。请示例代码!

解决方案

我建议尝试以下程序。它使用两个Linux进程来运行由父进程创建的两个进程。一旦一个孩子结束,父母会得到通知并终止对方。研究出现在include部分的系统调用的文档当然是理解代码所必需的。

  // fork 
#include< unistd.h>

//等待,杀死
#include< sys / types.h>

//等待
#include< sys / wait.h>

//杀死
#include< signal.h>

// exit
#include< stdlib.h>

// printf
#include< stdio.h>

void proc(int id)
{
int seed = id;
int count = rand_r(& seed)%10;
printf(\ nprocess [%d]:采取%d步骤。,id,count);
int i;
for(i = 1; i <= count; ++ i){
printf(\ nprocess [%d]:step#%d,id,i);
sleep(1);
}
printf(\ nprocess [%d]:\tfinished。,id);
exit(1);


$ main(int argc,char * argv [])
{
//创建第一个子进程
pid_t p1 = fork() ;
if(0 == p1)
proc(1); // child不返回

//创建第二个子进程
pid_t p2 = fork();
if(0 == p2)
proc(2); //子节点不返回

//这是父进程
//等待子节点终止
pid_t p_terminated = wait(NULL);

//声明结果
//终止另一个孩子
if(p1 == p_terminated){
puts(\\\
parent:\ tprocess [1 ]先完成。);
kill(p2,SIGKILL);
}

if(p2 == p_terminated){
puts(\\\
parent:\tprocess [2] first first。);
kill(p1,SIGKILL);






在我的机器上,程序产生以下输出:

 过程[1]:采取3个步骤。 

过程[2]:采取7个步骤。
process [1]:step#1
process [2]:step#1
process [1]:step#2
process [2]:step#2
过程[1]:步骤#3
过程[2]:步骤#3
过程[1]:完成。
parent:process [1]第一个完成。

这两个过程可能同时完成,因此它们都会打印 code>已完成消息,但即使在这种情况下,父级声明其中一个是第一个,并终止另一个。


I am an experienced C programmer who has never used threads or parallelism before. I've been reading about it, but I do not see an example of what I want.

I use the gcc C compiler on Mac and linux. I want to replace an important procedure X in my system with a new procedure X2 that will start up two methods, as two threads to be run on two different processors whenever the machine has multiple CPUs (most do these days).

These two methods may share a few global variables, but they will not write to any memory locations other than their own stacks. They will each call many other procedures in the system. I do not envision any other parallel processing.

As soon as either thread finishes, that's it! That's the answer. X2 should immediately kill the other thread and return the answer to whoever called X2.

Maybe I'm naive but I would think this is a well known use of threads. Example code please!

解决方案

I propose to try the following program. It uses two Linux processes to run two procedures, created by a parent process. As soon as one child finishes, the parent gets notified and terminates the other. Studying the documentation for the system calls appearing in the include section is of course necessary to understand the code.

// fork
#include <unistd.h>

// wait, kill
#include <sys/types.h>

// wait
#include <sys/wait.h>

// kill
#include <signal.h>

// exit
#include <stdlib.h>

//printf
#include <stdio.h>

void proc( int id)
{
  int seed= id;
  int count= rand_r( &seed) % 10;
  printf( "\nprocess[%d]: taking %d steps.", id, count);
  int i;
  for ( i= 1; i <= count; ++i) {
    printf( "\nprocess[%d]: step #%d", id, i);
    sleep( 1);
  }
  printf( "\nprocess[%d]:\tfinished.", id);
  exit( 1);
}

int main( int argc, char* argv[])
{
  // create 1st child process
  pid_t p1= fork();
  if ( 0 == p1)
    proc( 1); // child does not return

  // create 2nd child process
  pid_t p2= fork();
  if ( 0 == p2)
    proc( 2); // child does not return

  // this is the parent process
  // wait for a child to terminate
  pid_t p_terminated= wait( NULL); 

  // declare result
  // terminate the other child
  if ( p1 == p_terminated) {
    puts( "\nparent:  \tprocess[1] finished first.");
    kill( p2, SIGKILL);
  }

  if ( p2 == p_terminated) {
    puts( "\nparent:  \tprocess[2] finished first.");
    kill( p1, SIGKILL);
  }
}

On my machine, the program produces the following output:

process[1]: taking 3 steps.

process[2]: taking 7 steps.
process[1]: step #1
process[2]: step #1
process[1]: step #2
process[2]: step #2
process[1]: step #3
process[2]: step #3
process[1]:     finished.
parent:         process[1] finished first.

It is possible for the two processes to finish at the same time so that they both print their "finished" message, but even in this case the parent declares one of them to be the first, and terminates the other.

这篇关于结束gcc C Linux中的第一个成功的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆