[Mono-devel-list] TLS: normal versus __thread

Zoltan Varga vargaz at gmail.com
Thu Mar 24 13:12:08 EST 2005


                                                Hi,

  I think calling fork from managed code is a bad-bad idea, and it is
unlikely to
ever work.

                                                                Zoltan

On Thu, 24 Mar 2005 17:41:23 +0100, Michael Rasmussen <mir at miras.org> wrote:
> tor, 24 03 2005 kl. 17:25 +0100, skrev Zoltan Varga:
> 
> > No, its just an optimization.
> >
> Maybe, but it seems to be the only difference in the runtime
> environments between a working solution with fork.
> 
> You are welcome to try this small code snippet:
> 
> // project created on 21-03-2005 at 15:16
> using System;
> using System.Net.Sockets;
> using System.IO;
> using Mono.Posix;
> 
> namespace TCPSocketServer
> {
> 
>         public class Server
>         {
>                 private StreamWriter streamWriter;
>                 private StreamReader streamReader;
> 
>                 public Server(int port)
>                 {
>                         System.Int32 pid, sid;
> 
>                         pid = Syscall.fork();
>                         if (pid < 0) {
>                         Syscall.exit(1);
>                         }
>                         /* If we got a good PID, then
>                            we can exit the parent process. */
>                         if (pid > 0) {
>                                 Console.WriteLine("Parent: Signing off!");
>                                 Console.WriteLine("Parent PID: " + pid);
>                         Syscall.exit(0);
>                         }
>                         /* Create a new SID for the child process */
>                         sid = Syscall.setsid();
>                         if (sid < 0) {
>                                 Console.WriteLine("Could not get a pid for child");
>                         Syscall.exit(1);
>                         }
>                         Console.WriteLine("Client running as deamon with pid " + sid);
>                         CreateSocket(port);
>                         if (!StartServer()) {
>                                 Console.WriteLine("Unable to start server");
>                                 Syscall.exit(1);
>                         }
>                 }
> 
>                 public static void Main(string[] args)
>                 {
>                         int port;
> 
>                         if (args.Length > 0)
>                                 port = Convert.ToInt32(args[0]);
>                         else
>                                 port = 1234;
>                         new Server(port);
>                 }
> 
>                 private bool CreateSocket(int port)
>                 {
>                         TcpListener tcpListener = new TcpListener(port);
>                         tcpListener.Start();
>                         Console.WriteLine("Server started");
>                         Socket socket = tcpListener.AcceptSocket();
> 
>                         try
>                         {
>                                 if (socket.Connected) {
>                                         Console.WriteLine("Client connected");
>                                         NetworkStream networkStream = new NetworkStream(socket);
>                                         streamWriter = new StreamWriter(networkStream);
>                                         streamReader = new StreamReader(networkStream);
>                                 }
>                         }
>                         catch (Exception e)
>                         {
>                                 Console.WriteLine(e.StackTrace);
>                                 return false;
>                         }
>                         return true;
>                 }
> 
>                 private bool StartServer()
>                 {
>                         while (true) {
>                                 string input = streamReader.ReadLine();
>                                 Console.WriteLine("Client: " + input);
>                                 streamWriter.WriteLine("Hi dude. You sent this:\n" + input);
>                                 streamWriter.Flush();
>                         }
>                 }
>         }
> }
> 
> Hilsen/Regards
> Michael Rasmussen
> 
> Get my public GnuPG keys:
> michael  rasmussen  cc
> http://keyserver.veridis.com:11371/pks/lookup?op=get&search=0xD3C9A00E
> mir  datanom  net
> http://keyserver.veridis.com:11371/pks/lookup?op=get&search=0xE501F51C
> mir  miras  org
> http://keyserver.veridis.com:11371/pks/lookup?op=get&search=0xE3E80917
> --------------------------------------------------------------
> Debian Hint #25: If you would like to thank a maintainer for handling an
> issue, check out reportbug --kudos.
> 
> 
>



More information about the Mono-devel-list mailing list