c语言 linux 中 poll 的参数

Python011

c语言 linux 中 poll 的参数,第1张

分类: 电脑/网络 >>程序设计 >>其他编程语言

问题描述:

#include <sys/poll.h>int poll(struct pollfd *ufds, unsigned int nfds, int timeout)struct pollfd{int fd/* 想查询的文件描述符. */short int events/* fd 上,我们感兴趣的事件*/short int revents/* Types of events that actually occurred. */}请问第二个参数unsigned int nfds的含义,它传递什么信息?

解析:

ufds 指向 struct pollfd 数组

nfds 指定 pollfd 数组元素的个数,也就是要监测几个 pollfd

sizeof(argv)这个

函数

返回的,只是argv

数组

地址

长度

指针,就是存放某

变量

的地址,地址在

内存

中永远都是只占4个

字节

3个线程使用的都是同一个info

代码 Info_t *info = (Info_t *)malloc(sizeof(Info_t))只创建了一个info

pthread_create(&threads[i],NULL,calMatrix,(void *)info)三个线程使用的是同一个

我把你的代码改了下:

#include <stdio.h>

#include <stdlib.h>

#include <pthread.h>

int mtc[3] = { 0 } // result matrix

typedef struct

{

    int prank

    int *mta 

    int *mtb

}Info_t

void* calMatrix(void* arg)

{

    int i

    Info_t *info = (Info_t *)arg

    int prank = info->prank

    fprintf(stdout,"calMatrix : prank is %d\n",prank)

    for(i = 0 i < 3 i++)

        mtc[prank] += info->mta[i] * info->mtb[i]

    return NULL

}

int main(int argc,char **argv)

{

    int i,j,k = 0

    int mta[3][3]

    int mtb[3] = { 1 }

    Info_t *info = (Info_t *)malloc(sizeof(Info_t)*3)

    for(i = 0 i < 3 i++)

        for(j = 0 j < 3 j++)

            mta[i][j] = k++

    /* 3 threads */

    pthread_t *threads = (pthread_t *)malloc(sizeof(pthread_t)*3)

    fprintf(stdout,"\n")fflush(stdout)

    for(i = 0 i < 3 i++)

    {

        info[i].prank = i

        info[i].mta = mta[i]

        info[i].mtb = mtb

        pthread_create(&threads[i],NULL,calMatrix,(void *)(&info[i]))

    }

    for(i = 0 i < 3 i++)

        pthread_join(threads[i],NULL)

    fprintf(stdout,"\n==== the matrix result ====\n\n")

    fflush(stdout)

    for(i = 0 i < 3 i++)

    {

        fprintf(stdout,"mtc[%d] = %d\n",i,mtc[i])

        fflush(stdout)

    }

    return 0

}

矩阵的计算我忘记了,你运行看看结果对不对