Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
341 views
in Technique[技术] by (71.8m points)

c - Modify PID manager for multi-threading

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("
Bitmap Data Structure initialized.
");
        int id = 0, i = 0;

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

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

/* 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("
Invalid PID: It should lie between 300 and 3000.");
        return;
    }
    int i = pid - MIN_PID;
    unsignedChar[i/CB] &= ~(1 << (i & (CB-1)));
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...