tcp/ip chat

This is a discussion on tcp/ip chat within the C Programming forums, part of the General Programming Boards category; im trying to implement a multiclient tcp/ip chat and in the server side im spawning threads for every client. but ...

  1. #1
    Daring to be a guru
    Join Date
    Dec 2007
    Posts
    14

    tcp/ip chat

    im trying to implement a multiclient tcp/ip chat and in the server side im spawning threads for every client. but during execution the processor is executing only one thread and the main thread. is there any way to manually switch the threads for cpu time after some time say 10secs or like that?

  2. #2
    Registered User
    Join Date
    Nov 2007
    Location
    Bangalore, India
    Posts
    24
    Once created the OS should schedule the thread automatically unless they are blocked/suspended.
    Which OS are you using?

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,545
    On Windows, no. You can set priority to how much cpu time they should get and how often they should be rescheduled but nothing aside from that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Daring to be a guru
    Join Date
    Dec 2007
    Posts
    14
    im doin it in unix using gcc.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    Presumably each thread is waiting on say a socket for something interesting to happen?

    My preferred way would be to use select() with a timeout if you want each thread to run every so often, even if there is no network traffic.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Daring to be a guru
    Join Date
    Dec 2007
    Posts
    14
    hmmm..... me even thinkin so to use select but i almost did the entire thing based on thread now have to find a way to change the existin code.....

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    select() with a timeout can do a lot of what most people rush headlong into threads as being "the only solution".
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Daring to be a guru
    Join Date
    Dec 2007
    Posts
    14
    is there any way really to schedule threads manually??

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,545
    I doubt there would be in any operating system. It messes up thread scheduling.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Daring to be a guru
    Join Date
    Dec 2007
    Posts
    14
    ok........ no other go have to go up with select call only.....


    thank you all.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by gopal_85 View Post
    is there any way really to schedule threads manually??
    Why would you want to do that? Or perhaps I should put it another way: Why would any sensible OS producer want to do that? The best place to decide which thread to run has to be in the OS - it is the only part of the system that KNOWS what is going on.

    You can make threads go to sleep on your say so, but the only way that a particular thread will be made running is if the OS decides that it should run.

    What is making the "only one thread" situation? Are you perhaps spinning around performing some waiting task by polling if something is available?

    Also, in Unix/Linux/Windows, multiple threads should be time-sharing, so even if you start 10 threads, they will be sharing the CPU, and the time-quanta that one thread can use up at any given time before another runnable thread can be run should be much less than a second - so if 10 threads can run, they should all run at least once a second, probably more often. So you may want to look into why your threads are NOT behaving this way - perhaps you are causing the other threads to block for some reason [this may include reading from a shared resource which leads to ONE thread getting most of the time].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    You are probably using blocking sockets and one or more of your sockets are blocking causing a threadlock. Your threads should behave as matsp described it and if they are not something else is wrong.

  13. #13
    Daring to be a guru
    Join Date
    Dec 2007
    Posts
    14
    Yeah what matsp told is right there was a block and as i removed that block it is now working properly...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Requesting Java Chat Client Maintenence
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 04-02-2003, 12:57 PM
  2. Chat server/client trial -- anyone interested?
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 01-23-2003, 09:47 PM
  3. SO close to finishing chat program...!
    By Nakeerb in forum C++ Programming
    Replies: 13
    Last Post: 10-26-2002, 12:24 PM
  4. Chat program advice
    By gogo in forum C++ Programming
    Replies: 11
    Last Post: 01-04-2002, 09:45 AM
  5. Rough Portable Chat Design Sketch
    By ggs in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-27-2001, 07:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21