Thread: pthread question

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    77

    pthread question

    g'day,
    I've a question about pthread or fork functions. For example, I've got parent copy my programm which ran from root privileges, than it could be forked or new pthread create with new pid and use after that from user. So, my question to implies with access permission from user to data which's has only root permissions if I use it before in root pthread or fork?

  2. #2
    Registered User oteros's Avatar
    Join Date
    Mar 2009
    Location
    Madrid, Spain
    Posts
    7

    fork & phtread

    Hello.
    When you pthread a function, you stay in same 'main' process, but under different 'process-id'. So, you share all resources between processes. A new 'pthreaded process' is not the chidren of they creator, is best to regard between us as 'brothers?', because no jerarquical levels exists. The process user id & memory are the same between all pthreaded processes, as files, etc, but the call stack.

    When you fork, a totaly new process is created, sharing a lot on info as files, shared memory, etc, but new virtual region for the process is allocated. As you as forked a new process under 'certain user', the children is working using same permissions, until you explicity changes with a special syscall function, if you have the permisions to do. Children have a 'copy' of memory of creator, but none can access to the other memory.

    Best regards.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    77
    Hm, right, i understood that. But when we use fork it's create background process, than pthread don't do it. With in similar of this occurs we can use mix with pthreads and fork, because pthread worked too mutch faster and usage shared memory more simple than fork with pipes. For instanse, fork would doing background process and next use pthreads if it's MP-system hardware or just no more than two threads on single core CPU. Possible can do backgroung process without fork usage?
    btw, which's way I can detect how many CPU core's has my hardware, restricted pthreads value no more than 2 on that system and queueing data for them?

  4. #4

  5. #5
    Registered User oteros's Avatar
    Join Date
    Mar 2009
    Location
    Madrid, Spain
    Posts
    7

    Smile In begin, no matter nucleous number.

    You must think on MP even any number on machine nucleus. You will take advantage over machine waits. Y you want a isolates processes, you must use fork. If you only want a background processes, best use threads.

    Why?.

    Because the threads shares memory on the process, all is more easy. Mutexes use are more simple to implement that SYS V IPC, and much less expensive. (I not says cheaper, well ..)

    Ex.: If you want submit a process and free the process terminal ( simple daemon), best use a simple fork and end the caller. The children continues execution until they ends.
    --
    If you want share data on a simple way, or do simple sincro, best use threads & mutexes. Use of mutexes are easy than the use of semafore, and very rich.
    --
    If you want a complex system of daemons that shared memory between and executes isolatelly, you must use fork, threads in each process, a mix of IPC & mutexes. You can evaluate the use of mutexes on SHARED MEMORY instead semafores. Any way, be sure that you need at least a semafore to control the first SHR MERM access. (yes, you can use SHRMEM as a place to build mutexes dinamically - the number of SYS V semafores is not large -).

    Rule: Be KISS.

    Best Regards.

  6. #6
    Registered User oteros's Avatar
    Join Date
    Mar 2009
    Location
    Madrid, Spain
    Posts
    7

    How many CPU's ?

    Well,

    A simple way to do this is read the /proc/....BLA...BLA/ directories to locate each CPU, because each CPU in system have they proper /proc/.. directory: I don't remember now how subdirectories are called, but a simple 'dir' show's.

    B.R.
    Here is the truth
    -----------------------------------------------------------------
    trancas:/home/gonzalo# cat /proc/cpuinfo
    processor : 0
    vendor_id : GenuineIntel
    cpu family : 6
    model : 15
    model name : Intel(R) Core(TM)2 Quad CPU Q6600 @ 2.40GHz
    stepping : 11
    cpu MHz : 1600.000
    cache size : 4096 KB
    physical id : 0
    siblings : 4
    core id : 0
    cpu cores : 4
    apicid : 0
    initial apicid : 0
    fpu : yes
    fpu_exception : yes
    cpuid level : 10
    wp : yes
    flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx lm constant_tsc arch_perfmon pebs bts rep_good pni monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr lahf_lm
    bogomips : 4803.59
    clflush size : 64
    cache_alignment : 64
    address sizes : 36 bits physical, 48 bits virtual
    power management:

    processor : 1
    vendor_id : GenuineIntel
    cpu family : 6
    model : 15
    model name : Intel(R) Core(TM)2 Quad CPU Q6600 @ 2.40GHz
    stepping : 11
    cpu MHz : 1600.000
    cache size : 4096 KB
    physical id : 0
    siblings : 4
    core id : 2
    cpu cores : 4
    apicid : 2
    initial apicid : 2
    fpu : yes
    fpu_exception : yes
    cpuid level : 10
    wp : yes
    flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx lm constant_tsc arch_perfmon pebs bts rep_good pni monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr lahf_lm
    bogomips : 4800.06
    clflush size : 64
    cache_alignment : 64
    address sizes : 36 bits physical, 48 bits virtual
    power management:

    processor : 2
    vendor_id : GenuineIntel
    cpu family : 6
    model : 15
    model name : Intel(R) Core(TM)2 Quad CPU Q6600 @ 2.40GHz
    stepping : 11
    cpu MHz : 1600.000
    cache size : 4096 KB
    physical id : 0
    siblings : 4
    core id : 3
    cpu cores : 4
    apicid : 3
    initial apicid : 3
    fpu : yes
    fpu_exception : yes
    cpuid level : 10
    wp : yes
    flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx lm constant_tsc arch_perfmon pebs bts rep_good pni monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr lahf_lm
    bogomips : 4800.07
    clflush size : 64
    cache_alignment : 64
    address sizes : 36 bits physical, 48 bits virtual
    power management:

    processor : 3
    vendor_id : GenuineIntel
    cpu family : 6
    model : 15
    model name : Intel(R) Core(TM)2 Quad CPU Q6600 @ 2.40GHz
    stepping : 11
    cpu MHz : 1600.000
    cache size : 4096 KB
    physical id : 0
    siblings : 4
    core id : 1
    cpu cores : 4
    apicid : 1
    initial apicid : 1
    fpu : yes
    fpu_exception : yes
    cpuid level : 10
    wp : yes
    flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx lm constant_tsc arch_perfmon pebs bts rep_good pni monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr lahf_lm
    bogomips : 4800.00
    clflush size : 64
    cache_alignment : 64
    address sizes : 36 bits physical, 48 bits virtual
    power management:

    trancas:/home/gonzalo#
    -----------------------------------------------------------------
    Last edited by oteros; 04-06-2009 at 11:40 AM. Reason: mistake about information

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> A simple way to do this is read the /proc/...
    This will tell you the CPU's and core's that physically exist.
    But only sched_getaffinity() will tell you what core's your process/thread is allowed to run on.

    gg

  8. #8
    Registered User oteros's Avatar
    Join Date
    Mar 2009
    Location
    Madrid, Spain
    Posts
    7

    I don't disagree ...

    Yes, is true. You answer a question, I another.
    Best regards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The Pthread Hell
    By matott in forum Linux Programming
    Replies: 1
    Last Post: 04-10-2005, 05:59 AM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. pthread question
    By rotis23 in forum Linux Programming
    Replies: 10
    Last Post: 04-07-2004, 02:57 AM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM