Assignments


General Linux programming resources

Advanced programming

 

 

Socket programming resources

 

http://members.tripod.com/~Xengren/linnet/index.html

http://www.linuxjournal.com/article.php?sid=2333
http://www.linuxgazette.com/issue47/bueno.html
http://www-106.ibm.com/developerworks/linux/library/l-rt6/?t=gr,Redhat=Sockets
http://www.linuxjournal.com/article.php?sid=2333
http://pont.net/socket/index.html
http://www.cs.uno.edu/~golden/teach.html
 
 
 
Example
 
/* client.c */
 
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
 
main(int argc,char *argv[])
{
   int create_socket;
   int bufsize = 1024;
   char *buffer = malloc(bufsize);
   struct sockaddr_in address;
   
   if ((create_socket = socket(AF_INET,SOCK_STREAM,0)) > 0)
     printf("The Socket was created\n");
   address.sin_family = AF_INET;
   address.sin_port = htons(15000);
   inet_pton(AF_INET,argv[1],&address.sin_addr);
   
   if (connect(create_socket,(struct sockaddr *)&address,sizeof(address)) == 0)
     printf("The connection was accepted with the server %s...\n",inet_ntoa(address.sin_addr));
   do{
      recv(create_socket,buffer,bufsize,0);
      printf("Message recieved: %s\n",buffer);
      if (strcmp(buffer,"/q")){
         printf("Message to send: ");
         gets(buffer);
         send(cria_socket,buffer,bufsize,0);
      }
   }while (strcmp(buffer,"/q"));
   close(create_socket);
}
 
 
 
 
/* server.c */
 
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>      
 
main()
{
   int cont,create_socket,new_socket,addrlen;
   int bufsize = 1024;
   char *buffer = malloc(bufsize);
   struct sockaddr_in address;
   
   printf("\x1B[2J");
   if ((create_socket = socket(AF_INET,SOCK_STREAM,0)) > 0)
     printf("The socket was created\n");
   address.sin_family = AF_INET;
   address.sin_addr.s_addr = INADDR_ANY;
   address.sin_port = htons(15000);
   if (bind(create_socket,(struct sockaddr *)&address,sizeof(address))== 0)
     printf("Binding Socket\n");
   listen(create_socket,3);
   addrlen = sizeof(struct sockaddr_in);
   new_socket = accept(create_socket,(struct sockaddr *)&address,&addrlen);
   if (new_socket > 0){
      printf("The Client %s is connected...\n",inet_ntoa(address.sin_addr));
      for(cont=1;cont<5000;cont++)
        printf("\x7");
   }
   do{
      printf("Message to send: ");
      gets(buffer);
      send(new_socket,buffer,bufsize,0);
      recv(new_socket,buffer,bufsize,0);
      printf("Message recieved: %s\n",buffer);
   }while(strcmp(buffer,"/q")); //user ‘q’ to quit
   close(new_socket);
   close(create_socket);
}
 

 

 

 

Threading

Java

http://www.javaworld.com/javaworld/jw-09-1998/jw-09-threads.html

http://www.javaworld.com/javaworld/jw-10-1998/jw-10-toolbox.html

http://www.javaworld.com/javaworld/jw-11-1998/jw-11-toolbox_p.html

… go to www.javaworld.com and search on threads

 

Perl

 

Linux C threads: examples for more detail see above in general programming

/***********************************************************************
* Code listing from "Advanced Linux Programming," by CodeSourcery LLC  *
* Copyright (C) 2001 by New Riders Publishing                          *
* See COPYRIGHT for license information.                               *
***********************************************************************/
 
#include 
#include 
 
/* Parameters to print_function.  */
 
struct char_print_parms
{
  /* The character to print.  */
  char character;
  /* The number of times to print it.  */
  int count;
};
 
/* Prints a number of characters to stderr, as given by PARAMETERS,
   which is a pointer to a struct char_print_parms.  */
 
void* char_print (void* parameters)
{
  /* Cast the cookie pointer to the right type.  */
  struct char_print_parms* p = (struct char_print_parms*) parameters;
  int i;
 
  for (i = 0; i < p->count; ++i)
    fputc (p->character, stderr);
  return NULL;
}
 
/* The main program.  */
 
int main ()
{
  pthread_t thread1_id;
  pthread_t thread2_id;
  struct char_print_parms thread1_args;
  struct char_print_parms thread2_args;
 
  /* Create a new thread to print 30000 x's.  */
  thread1_args.character = 'x';
  thread1_args.count = 30000;
  pthread_create (&thread1_id, NULL, &char_print, &thread1_args);
 
  /* Create a new thread to print 20000 o's.  */
  thread2_args.character = 'o';
  thread2_args.count = 20000;
  pthread_create (&thread2_id, NULL, &char_print, &thread2_args);
  
  /* Make sure the first thread has finished.  */
  pthread_join (thread1_id, NULL);
  /* Make sure the second thread has finished.  */
  pthread_join (thread2_id, NULL);
 
  /* Now we can safely return.  */
  return 0;
}

 

/***********************************************************************
* Code listing from "Advanced Linux Programming," by CodeSourcery LLC  *
* Copyright (C) 2001 by New Riders Publishing                          *
* See COPYRIGHT for license information.                               *
***********************************************************************/
 
#include 
#include 
#include 
 
/* An array of balances in accounts, indexed by account number. Critical section */
 
float* account_balances;
 
/* Transfer DOLLARS from account FROM_ACCT to account TO_ACCT.  Return
   0 if the transaction succeeded, or 1 if the balance FROM_ACCT is
   too small.  */
 
int process_transaction (int from_acct, int to_acct, float dollars)
{
  int old_cancel_state;
 
  /* Check the balance in FROM_ACCT.  */
  if (account_balances[from_acct] < dollars)
    return 1;
 
  /* Begin critical section.  */
  pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &old_cancel_state);
  /* Move the money.  */
  account_balances[to_acct] += dollars;
  account_balances[from_acct] -= dollars;
  /* End critical section.  */
  pthread_setcancelstate (old_cancel_state, NULL);
 
  return 0;
}