¿Cómo configurar los atributos de los subprocesos en Linux?

Now I want to create three processes in my program and there are several threads in each process.
And each thread is infinite task, which may sleep and be waked periodically. Besides, the process has some task to do.
Mis preguntas son las siguientes:

1) Do I need to set the threads as detached ? If I set the threads as detached , they seem not to run!! But, If threads as joinable, the process has to wait the threads to exit and it can't do its own work!! which one should I choose?

2)What's the scope of schedule policy ? I mean, if I set the schedule policy as FIFO, all the threads in the all processes are scheduled by FIFO policy? Or just the thread which is set with this attribute is scheduled by this policy?

3)What's the scope of thread priority? The thread priorities are just useful in the single process, and in another process, there exist another set of thread priorities ????? And they don't infect each other???

I would appreciate for your help! thank you!

preguntado el 10 de marzo de 12 a las 01:03

3 Respuestas

DETACHED OR JOINED: It depends on the type of requirement you need.

If you want the main executable thread(which is spawning new threads) need to continue on its work and no need to wait for the spawned thread return value, you can use DETACH.

If you need the main executable thread, to only wait for the return value and do not need to perform any other task on its own. You can use JOIN.

When a thread is created, it uses the default scheduling policy unless changed by the attribute, before calling pthread_create. Also after creation, dynamically you can change the scheduling policy. NOTE: Scheduling Policy affects threads with same priority.

Priority: you can change priority using pthread_setschedparam (also for scheduling policy). However, in Linux thread is also a light weight process. So, all the threads are priority are looked at entire process level, not within each process.

respondido 10 mar '12, 04:03

Do you mean that the whole program only have a schedule policy? And all the threads, no matter in which process, are regraded in the same set of priority in the whole program? - ciudad

priority and scheduling are set at thread level. Look at each thread as process in itself. If Process P1 has Th1,Th2 threads of sched policy Sched1 and Sched2. Process P2 has threads Th3,Th4 having sched policy Sched1,Sched2 respectively. So the OS schedules Th1,Th3 using Sched1 mechanism and Th2,Th4 uses Sched2 mechanism (assuming threads are having same priority). Similarly, If P1 has 5 threads of Pr1 and another 3 threads of Pr2. If P2 has 2 threds of Pr1. P1(5 threds)+P2(2 threads), each thread will get 1/7 of time slice for that priority by OS(assuming same scheduling policy). - vamsi

(1) You have a coding error. A detached thread gets a time slice like everything else. If it is not running then it is something you are doing. You should post your threadfunc and the function which creates the threads in another question.

It's impossible to say whether your threads should be joinable or detached without knowing what you are doing. The main benefit of joinable threads are you know when they finish and you can check the return data. If these aren't important to you there is no real advantage to making them joinable - other than it is marginally easier to create them because that is the default.

If you don't want to block in pthread_join there are strategies you can pursue. Your threads can set switches before they die, you can use condition variables, you can have a separate thread that joins the dead threads and so forth. Again, it is impossible to know what is the best strategy for your particular case.

(2 & 3) A thread inherits the schedule policy and priority of the thread that creates it and they remain that way unless you specifically change them. The policy/priority of threads in one process are not directly related to any other process.

respondido 10 mar '12, 02:03

I'm answering only to the first question:

No need to create the threads as detached, since you can simply join them at the end of the main process.

To create threads as detached you should first create an attribute and then use it as a parameter to pthread_create

pthread_t thread1;
pthread_attr_t attr;

int chk;

chk = pthread_attr_init(&attr);
printf("attr_init: %d\n",chk);

chk = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
printf("attr_setdetachstate: %d\n",chk);

chk = pthread_create(&thread1, &attr, function, NULL);

respondido 10 mar '12, 02:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.