int main( int argc, char *argv[] ) {
    char *str,*p;
    unsigned int secs = 30;
    unsigned int timer;
    int block = 100;
   
    if( argc == 2 ) {
        if (strcmp("-h", argv[1]) == 0){
            help_function(argv[0]);
            return 0;
        }else{
        block = strtol(argv[1], &p, 10);
        }
    }
    
    printf("PID = %d\n",getpid());
    
    for(int i = 0 ; i < block; i++){
        str = (char *) malloc(1024);
    }
    
    strcpy(str, "simple_test");
    
    printf("Reserving %d Kb of memory\n", block);
    printf("Value of String = %s\n", str);
    printf("Address = %u\n", str);
    
    timer = time(0) + secs;
    
    printf("Waiting for %d seconds\n", secs);
    while (time(0) < timer);
        
    free(str);
    return(0);
}
Exemplo n.º 2
0
int main(int argc, char *argv[]) {
	// Define local variables in the main
	char c;
	char *str;
	int on;
	int off;
	int num;

	if (argc >= 2) {
		if ( strcmp(argv[1], "--help") == 0 
			|| strcmp(argv[1], "-h") == 0 ) help_function(argc, argv);

		else if ( strcmp(argv[1], "--morse") == 0
				|| strcmp(argv[1], "-m") == 0) {
			printf("What character would you morse (0 to exit): ");
			scanf("%c", &c);
			if (c == '0') return 0;
			blinkMorseChar(c);
			return 0;
		}

		else if ( strcmp(argv[1], "--morsestring") == 0
				|| strcmp(argv[1], "-ms") == 0) {
			printf("What string would you morse (0 to exit): ");
			fgets(str, 1024, stdin);
			printf("%s", str);
			if (c == '0') return 0;
			blinkMorseString(str);
		}

		else if ( strcmp(argv[1], "--blink") == 0
				|| strcmp(argv[1], "-b") == 0) {
			printf("Light on: ");
			scanf("%d", &on);
			printf("Light off: ");
			scanf("%d", &off);
			printf("How often the light is on: ");
			scanf("%d", &num);
			blinkLight(on, off, num);
		}

	} else {
		help_function(argc, argv);
		return 0;
	}
	return -1;
}
int main (int argc, char **argv)
{
 	int client_accept,number,server_socket;
	pid_t pid;
 	socklen_t size_clientr;
 	char buffer[MAX];
	char *temp1,*temp2;
	int port;
	FILE *input;
	char port_number[MAX];
	int data;
	time_t currentTime;
 	struct sockaddr_in clientr,serverr;

 	if (argc != 2){						
 	 	usage();
 		exit(1);
 	}
 	/*SİGNAL*/
	signal(SIGPIPE, SIG_IGN);
        signal(SIGINT, handle);

	server_socket = socket(AF_INET , SOCK_STREAM ,0);
	if (server_socket == -1)
    	{
        	perror("Could not create socket!!!");
        }

 	serverr.sin_family = AF_INET;
 	serverr.sin_addr.s_addr = htonl(INADDR_ANY);
 	if(atoi(argv[1]) <= 1024){
		perror("Port number must be greater than 1024");
		exit(2);
 	}
 	serverr.sin_port = htons(atoi(argv[1]));
	
	if( bind(server_socket,(struct sockaddr *)&serverr , sizeof(serverr)) < 0)
    	{
		perror("Bind Error!!!");
		return 1;
    	}

 	listen (server_socket, MAX_CLIENT);
	printf("waiting for connections...\n");

 	while(1){
		currentTime = time( NULL );
		fputs (ctime (&currentTime), stdout);
		size_clientr = sizeof(clientr);
  
		client_accept = accept(server_socket, (struct sockaddr *)&clientr, (socklen_t*)&size_clientr);

		if (client_accept < 0)
	        {
			perror("Client accept failed!!!");
			return 1;
		}
		printf("Connection accepted...\n");
		
  		/*
		*Child created for client requests
		*/

  		if ((pid = fork ()) == 0 ){
			close (server_socket);
			port = 1024;				
  			while ((number = recv(client_accept, buffer, MAX,0)) > 0)  {
   
				   temp1 = buffer;
				   temp2 = strtok(temp1," ");

				   if(strcmp("help\n",buffer)==0){
					
					help_function();

				   }if(strcmp("listServer\n",buffer)==0){
					
					listServer_function(port, argv[1],client_accept);
	
   				   }if(strcmp("lsClients\n",buffer)==0){

				   }
			
			}
	       } 
}

 	//close(client_accept);
}