Latest Post

c++ - substitute for fork()ing? in windows - Stack Overflow


I've been following Beej Networking guide and in the server section there is portion of code where it has called a function fork().


if (!fork()) { // this is the child process close(sockfd); // child doesn't need the listener if (send(new_fd, "Hello, world!", 13, 0) == -1) perror("send"); close(new_fd); exit(0);

I'm on a windows machine and cant get that part working. What can I do to solve this?. My code is as follows.


/* Server */ #define _WIN32_WINNT 0x501 #include <iostream> #include <windows.h> #include <winsock2.h> #include <ws2tcpip.h> #include <stdio.h> #include <sys/types.h> using namespace std; const int winsockVersion = 2; #define BACKLOG 10 #define PORT "3000" int main(void){ WSADATA wsadata; if (WSAStartup(MAKEWORD(winsockVersion,0),&wsadata) == 0){ cout<<"-WSAStartup initialized..." << endl; int status; int sockfd, new_fd; const char yes = '1'; struct addrinfo hints, *res,*loop_find; struct sockaddr_storage their_addr; socklen_t addr_size; memset(&hints,0,sizeof hints); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; if ( (status = getaddrinfo(NULL,PORT,&hints,&res)) == 0 ){ cout<<"-Call to get addrinfo successful!." << endl; } for (loop_find = res; loop_find!=NULL; loop_find = loop_find->ai_next){ if ( (sockfd = socket(loop_find->ai_family,loop_find->ai_socktype,loop_find->ai_protocol) ) == -1 ){ cout<<"-Could not create socket." << endl; continue; }else{ cout<<"-Socket Created." << endl; } //clearing in use ports. if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) { cout<<"-Couldnt clear blocked port." << endl; perror("setsockopt"); exit(1); } if( bind(sockfd,loop_find->ai_addr,loop_find->ai_addrlen) == -1 ){ closesocket(sockfd); perror("server: bind"); continue; } break; } if (listen(sockfd,BACKLOG) != -1){ cout<<"-Listening for incoming connections."; } //accept loop. while(true){ socklen_t addr_size = sizeof their_addr; new_fd = accept(sockfd,(sockaddr*)&their_addr,&addr_size); if ( new_fd == -1 ){ perror("accept"); continue; } struct sockaddr new_addr; int len = sizeof new_addr; getpeername(new_fd,&new_addr,&len); cout<<"-Connected to " << new_addr.sa_data << endl; if(!fork()){ //this is a child process closesocket(sockfd); if (send(new_fd,"hello world!!",13,0) == -1){ perror("send"); closesocket(new_fd); exit(0); } } closesocket(new_fd); } } //clear stuff if( WSACleanup() != 0){ cout<<"-WSACleanup unsuccessful" << endl; }else{ cout<<"-WSACleanup successful" << endl; } return 0; }



fork() obviously doesn't exist on Windows. Instead you'll need to create a new thread, or a whole new process.




Contrary to both existing answers (from OJ and Vincent Robert) fork() does exist on high-end versions of Windows. It's part of Subsystem for UNIX-based Applications (SUA) earlier called Microsoft Windows Services for UNIX (SFU), earlier called Interix.


Citing http://en.wikipedia.org/wiki/Interix, SUA is available on



  • Windows Server 2003 R2 (all editions) - version 5.2

  • Windows Vista (Ultimate and Enterprise editions) - version 6.0

  • Windows Server 2008 (all editions) - version 6.0

  • Windows Server 2008 R2 and Microsoft Windows 7 - version 6.1


All you have to do to use fork() is to install free SUA SDK. Depending on your target system you need one of the following:



You can also take a look at Does Interix implement fork()?




fork does not exist on Windows. You have to use a Window specific API called CreateProcess.


Contrary to fork, CreateProcess needs the path to the EXE. You can retrieve the path of the current EXE by calling GetModuleFileName with a NULL parameter.



I've been following Beej Networking guide and in the server section there is portion of code where it has called a function fork().


if (!fork()) { // this is the child process close(sockfd); // child doesn't need the listener if (send(new_fd, "Hello, world!", 13, 0) == -1) perror("send"); close(new_fd); exit(0);

I'm on a windows machine and cant get that part working. What can I do to solve this?. My code is as follows.


/* Server */ #define _WIN32_WINNT 0x501 #include <iostream> #include <windows.h> #include <winsock2.h> #include <ws2tcpip.h> #include <stdio.h> #include <sys/types.h> using namespace std; const int winsockVersion = 2; #define BACKLOG 10 #define PORT "3000" int main(void){ WSADATA wsadata; if (WSAStartup(MAKEWORD(winsockVersion,0),&wsadata) == 0){ cout<<"-WSAStartup initialized..." << endl; int status; int sockfd, new_fd; const char yes = '1'; struct addrinfo hints, *res,*loop_find; struct sockaddr_storage their_addr; socklen_t addr_size; memset(&hints,0,sizeof hints); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; if ( (status = getaddrinfo(NULL,PORT,&hints,&res)) == 0 ){ cout<<"-Call to get addrinfo successful!." << endl; } for (loop_find = res; loop_find!=NULL; loop_find = loop_find->ai_next){ if ( (sockfd = socket(loop_find->ai_family,loop_find->ai_socktype,loop_find->ai_protocol) ) == -1 ){ cout<<"-Could not create socket." << endl; continue; }else{ cout<<"-Socket Created." << endl; } //clearing in use ports. if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) { cout<<"-Couldnt clear blocked port." << endl; perror("setsockopt"); exit(1); } if( bind(sockfd,loop_find->ai_addr,loop_find->ai_addrlen) == -1 ){ closesocket(sockfd); perror("server: bind"); continue; } break; } if (listen(sockfd,BACKLOG) != -1){ cout<<"-Listening for incoming connections."; } //accept loop. while(true){ socklen_t addr_size = sizeof their_addr; new_fd = accept(sockfd,(sockaddr*)&their_addr,&addr_size); if ( new_fd == -1 ){ perror("accept"); continue; } struct sockaddr new_addr; int len = sizeof new_addr; getpeername(new_fd,&new_addr,&len); cout<<"-Connected to " << new_addr.sa_data << endl; if(!fork()){ //this is a child process closesocket(sockfd); if (send(new_fd,"hello world!!",13,0) == -1){ perror("send"); closesocket(new_fd); exit(0); } } closesocket(new_fd); } } //clear stuff if( WSACleanup() != 0){ cout<<"-WSACleanup unsuccessful" << endl; }else{ cout<<"-WSACleanup successful" << endl; } return 0; }


fork() obviously doesn't exist on Windows. Instead you'll need to create a new thread, or a whole new process.



Contrary to both existing answers (from OJ and Vincent Robert) fork() does exist on high-end versions of Windows. It's part of Subsystem for UNIX-based Applications (SUA) earlier called Microsoft Windows Services for UNIX (SFU), earlier called Interix.


Citing http://en.wikipedia.org/wiki/Interix, SUA is available on



  • Windows Server 2003 R2 (all editions) - version 5.2

  • Windows Vista (Ultimate and Enterprise editions) - version 6.0

  • Windows Server 2008 (all editions) - version 6.0

  • Windows Server 2008 R2 and Microsoft Windows 7 - version 6.1


All you have to do to use fork() is to install free SUA SDK. Depending on your target system you need one of the following:



You can also take a look at Does Interix implement fork()?



fork does not exist on Windows. You have to use a Window specific API called CreateProcess.


Contrary to fork, CreateProcess needs the path to the EXE. You can retrieve the path of the current EXE by calling GetModuleFileName with a NULL parameter.


test auto post for blogger 123 456 789 azerty azerty2 aqwxcv sdfg mmmm



User Guide

The Guava project contains several of Google's core libraries that we rely on in our Java-based projects: collections, caching, primitives support, concurrency libraries, common annotations, string processing, I/O, and so forth. Each of these tools really do get used every day by Googlers, in production services.

But trawling through Javadoc isn't always the most effective way to learn how to make best use of a library. Here, we try to provide readable and pleasant explanations of some of the most popular and most powerful features of Guava.

This wiki is a work in progress, and parts of it may still be under construction.

  • Basic utilities: Make using the Java language more pleasant.
    • Using and avoiding null: null can be ambiguous, can cause confusing errors, and is sometimes just plain unpleasant. Many Guava utilities reject and fail fast on nulls, rather than accepting them blindly.
    • Preconditions: Test preconditions for your methods more easily.
    • Common object methods: Simplify implementing Object methods, like hashCode() and toString().
    • Ordering: Guava's powerful "fluent Comparator" class.
    • Throwables: Simplify propagating and examining exceptions and errors.
  • Collections: Guava's extensions to the JDK collections ecosystem. These are some of the most mature and popular parts of Guava.
    • Immutable collections, for defensive programming, constant collections, and improved efficiency.
    • New collection types, for use cases that the JDK collections don't address as well as they could: multisets, multimaps, tables, bidirectional maps, and more.
    • Powerful collection utilities, for common operations not provided in java.util.Collections.
    • Extension utilities: writing a Collection decorator? Implementing Iterator? We can make that easier.
  • Caches: Local caching, done right, and supporting a wide variety of expiration behaviors.
  • Functional idioms: Used sparingly, Guava's functional idioms can significantly simplify code.
  • Concurrency: Powerful, simple abstractions to make it easier to write correct concurrent code.
    • ListenableFuture: Futures, with callbacks when they are finished.
    • Service: Things that start up and shut down, taking care of the difficult state logic for you.
  • Strings: A few extremely useful string utilities: splitting, joining, padding, and more.
  • Primitives: operations on primitive types, like int and char, not provided by the JDK, including unsigned variants for some types.
  • Ranges: Guava's powerful API for dealing with ranges on Comparable types, both continuous and discrete.
  • I/O: Simplified I/O operations, especially on whole I/O streams and files, for Java 5 and 6.
  • Hashing: Tools for more sophisticated hashes than what's provided by Object.hashCode(), including Bloom filters.
  • EventBus: Publish-subscribe-style communication between components without requiring the components to explicitly register with one another.
  • Math: Optimized, thoroughly tested math utilities not provided by the JDK.
  • Reflection: Guava utilities for Java's reflective capabilities.
  • Tips: Getting your application working the way you want it to with Guava.

NOTE: To discuss the contents of this wiki, please just use the guava-discuss mailing list.