修改PID经理多线程 [英] Modify PID manager for multi-threading

查看:175
本文介绍了修改PID经理多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的PID经理。我想所以它使用多线程对其进行修改。因此,我想每个线程请求PID(也许创建20个左右,像我在while循环do),睡的一段随机时间(我假设使用睡眠()),然后释放PID(类似你见下文)。

我是一个新手,总当谈到C和Linux的线程,所以如果有人能帮助我相应地修改此。我会克隆()函数调用启动?

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&limits.h中GT;#定义MIN_PID 300
#定义MAX_PID 5000
CB的#define CHAR_BITINT SZ = MAX_PID - MIN_PID + 1;无符号字符* unsignedChar;诠释allocate_map();
INT allocate_pid();
无效release_pid(INT PID);诠释的main()
{
    INT地图= allocate_map();
    如果(地图== 1){
        的printf(\\ nBitmap数据结构初始化\\ n);
        INT ID = 0,I = 0;    //创建20个进程
        而(ⅰ&下; 20){
            INT VAL = allocate_pid();
            的printf(\\ nProcess%D:PID =%d个我+ 1,VAL);
            我++;
        }
    //释放出几道工序
    release_pid(303);的printf(\\ nProcess 303释放。);
    release_pid(308);的printf(\\ nProcess 308释放。);
    release_pid(309);的printf(\\ nProcess 309释放。);    //这个发布后分配了几个流程
        INT VAL = allocate_pid();的printf(\\ nProcess%D:PID =%d个+ +我,VAL); //应该是303
    VAL = allocate_pid();的printf(\\ nProcess%D:PID =%d个\\ n,++我,VAL); //应该是308
    }
    其他的printf(\\ nFailed初始化数据结构\\ n);
}/ *创建和初始化一个位图数据结构重新presenting的PID;
 返回-1不成功,1成功* /
诠释allocate_map(){
    unsignedChar =(无符号字符*)malloc的((SZ + CB-1)/ CB * sizeof的(炭));
    如果(unsignedChar)返回1;
    返回-1;
}/ *分配并返回一个pid;返回-1
如果无法分配一个PID(所有PID都在使用)* /
INT allocate_pid(){
    INT I = 0;
    INT PID = unsignedChar [I / CB]放大器; (1 <<;&下;(ⅰ及(CB-1)));
    而(PID!= 0){
        我++;
        PID = unsignedChar [I / CB]放大器; (1 <<;&下;(ⅰ及(CB-1)));
        }    如果(1 + MIN_PID&GT; MAX_PID)返回-1;
    unsignedChar [I / CB] | = 1&LT;&LT; (ⅰ及(CB-1));
    返回1 + MIN_PID;
}/ *发布一个pid给出PID参数的* /
无效release_pid(INT PID){
    如果(PID&所述; 300){
        的printf(\\ n无效的PID:它应该300和3000。谎言);
        返回;
    }
    INT I = PID - MIN_PID;
    unsignedChar [I / CB]放大器; =〜(1 <<;&下;(ⅰ及(CB-1)));
}


解决方案

Posix线程是这样做的最简单和规范的方法。

维基百科得到了一个很好的例子了。

基本上,您通过 pthread_create的手段()线程实例,然后加入他们的行列,或等待它们完成通过在pthread_join()

注意,维基百科条目也说一些关于使用gcc编译。在 -pthread -lpthread 绝对必要存在的,否则你会得到未定义的引用。

Below is my PID manager. I want to modify it so it uses multi-threading. Therefore I would like for each thread to request a pid (maybe create 20 or so like I do in the while loop), sleep for a random period of time (I assume use sleep() ), and then release the pid (similar to what you see below).

I am a total newbie when it comes to threads in C and Linux so if someone can help me modify this accordingly. Would I start with the clone() function call?

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

#define MIN_PID 300
#define MAX_PID 5000
#define CB CHAR_BIT

int sz = MAX_PID - MIN_PID + 1;

unsigned char *unsignedChar;

int allocate_map();
int allocate_pid();
void release_pid(int pid);

int main() 
{
    int map = allocate_map();
    if (map == 1) {
        printf("\nBitmap Data Structure initialized.\n");
        int id = 0, i = 0;

    //create 20 processes
        while (i < 20) {
            int val = allocate_pid();
            printf("\nProcess %d: pid = %d", i+1, val);
            i++;
        }
    //release a few processes
    release_pid(303); printf("\nProcess 303 released.");
    release_pid(308); printf("\nProcess 308 released.");
    release_pid(309); printf("\nProcess 309 released.");

    //allocate a few more processes after this release
        int val = allocate_pid(); printf("\nProcess %d : pid = %d", ++i, val); //should be 303
    val = allocate_pid(); printf("\nProcess %d : pid = %d\n", ++i, val); //should be 308
    }
    else printf("\nFailed to initialize data structure.\n");
}

/* Creates and initializes a bitmap data structure for representing pids;
 returns —1 for unsuccessful, 1 for successful */
int allocate_map() {
    unsignedChar = (unsigned char*)malloc((sz+CB-1)/CB * sizeof(char));
    if (unsignedChar) return 1;
    return -1;
}

/* Allocates and returns a pid; returns -1
if it is unable to allocate a pid (all pids are in use) */
int allocate_pid() {
    int i = 0;
    int pid = unsignedChar[i/CB] & (1 << (i & (CB-1)));
    while (pid != 0) {
        i++;
        pid = unsignedChar[i/CB] & (1 << (i & (CB-1)));
        }

    if (i+MIN_PID > MAX_PID) return -1;
    unsignedChar[i/CB] |= 1 << (i & (CB-1));
    return i+MIN_PID;
}

/* Releases a pid given a pid parameter*/
void release_pid(int pid) {
    if (pid < 300) {
        printf("\nInvalid PID: It should lie between 300 and 3000.");
        return;
    }
    int i = pid - MIN_PID;
    unsignedChar[i/CB] &= ~(1 << (i & (CB-1)));
}

解决方案

Posix Threads are the simplest and canonical way to do this.

Wikipedia has got a nice example already.

Basically, you create thread instances by means of pthread_create() and join them thereafter or "wait for them to finish" via pthread_join().

Note that the Wikipedia entry also says something about compilation using gcc. The -pthread or -lpthread is strictly necessary there, otherwise you'll get undefined references.

这篇关于修改PID经理多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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