Pazartesi, Mayıs 29, 2006

Client/Server 3



Nihayet..
This project involves a very simple FTP application.
You are supposed to develop two programs, Client and Server, which have the above communication
scheme. Client program accepts the following user commands from the console:
get filename : requests a file. filename is the name of the requested file and it does not
include path information. This request is sent to the server through the
message queue. Upon this request, the server sends the file to the client
through the FIFO. If the requested file does not exist, the server does not
send anything.
put filename : requests uploading a file. After sending this request, the client
immediately sends the file to the server through the FIFO.
dir : request the list of the current directory (where server program is
executed).


--fullduplex.h file--
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "errno.h"
#include "ctype.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "sys/msg.h"
#include "fcntl.h"
#include "unistd.h"

#define NP1 "/tmp/np1"
#define NP2 "/tmp/np2"
#define MAX_BUF_SIZE 1024

struct my_msgbuf {
long mtype;
char mtext[200];
};

--client.c--

#include "fullduplex.h"
int main(int argc, char *argv[]){

int msgqid;
struct my_msgbuf msgbuf;
char command[10], filename[50];
FILE *file;
int rdfd, wrfd;
char readbuffer[MAX_BUF_SIZE];
int numread;

printf("client starts : ");
printf("%s %s\n", argv[0], argv[1]);
msgqid = atoi(argv[1]);

msgbuf.mtype = 1;//neden bilmiyorum
printf("\n--> ");
gets(msgbuf.mtext);
fflush(stdin);
if(msgsnd(msgqid,(struct my_msgbuf*)&msgbuf, sizeof(msgbuf), 0) == -1){
perror("msgsnd");
exit(1);
}
while(strcmp(msgbuf.mtext, "exit") != 0){
if(strcmp(msgbuf.mtext, "dir") == 0){
rdfd = open(NP2, O_RDONLY);
while((numread = read(rdfd, readbuffer, sizeof(readbuffer))) > 0){
readbuffer[numread] = '\0';
printf("%s", readbuffer);
}
printf("\n");
close(rdfd);
}
else{
sscanf(msgbuf.mtext, "%s %s\n", command, filename);
if(strcmp(command, "putfile") == 0){
file = fopen(filename, "r");
wrfd = open(NP1, O_WRONLY);
while((numread = fread(readbuffer, 1, sizeof(readbuffer), file)) > 0){
write(wrfd, readbuffer, numread);
}
fclose(file);
close(wrfd);
}
else{
if(strcmp(command, "getfile") == 0){
file = fopen(filename, "w");
rdfd = open(NP2, O_RDONLY);
while((numread = read(rdfd, readbuffer, sizeof(readbuffer))) > 0){
fwrite(readbuffer, 1, numread, file);
}
fclose(file);
close(rdfd);
}
else{
printf("unknown command\n");
}
}
}
printf("\n--> ");
gets(msgbuf.mtext);
fflush(stdin);
if(msgsnd(msgqid,(struct my_msgbuf*)&msgbuf, sizeof(msgbuf), 0) == -1){
perror("msgsnd");
exit(1);
}
}
printf("client ends\n");

return 1;
}

--server.c--
#include "fullduplex.h"
int main(void){

int ret_val;
key_t key;
int msgqid;
struct my_msgbuf msgbuf;
char command[10], filename[50];
FILE *file;
int rdfd, wrfd;
char readbuffer[MAX_BUF_SIZE];
int numread;

printf("server starts\n");
ret_val = mkfifo(NP1, S_IFIFO | 0666);
if((ret_val == -1) && (errno != EEXIST)){
perror("mkfifo");
exit(1);
}
ret_val = mkfifo(NP2, S_IFIFO | 0666);
if((ret_val == -1) && (errno != EEXIST)){
unlink(NP1);
perror("mkfifo");
exit(1);
}
if((key = ftok(".",'A')) == -1){
unlink(NP1);
unlink(NP2);
perror("ftok");
exit(1);
}
if((msgqid = msgget(key, IPC_CREAT | 0666)) == -1){
unlink(NP1);
unlink(NP2);
perror("msgget");
exit(1);
}
printf("fifo 1 (server read) : %s\n", NP1);
printf("fifo 1 (server write) : %s\n", NP2);
printf("message queue id : %d\n", msgqid);

if(msgrcv(msgqid, (struct my_msgbuf*)&msgbuf, sizeof(msgbuf), 0, 0) == -1){
unlink(NP1);
unlink(NP2);
msgctl(msgqid, IPC_RMID, 0);
perror("msgrcv");
exit(1);
}
while(strcmp(msgbuf.mtext, "exit") != 0){
printf("received command : %s\n", msgbuf.mtext);
if(strcmp(msgbuf.mtext, "dir") == 0){
file = popen("dir", "r");
wrfd = open(NP2, O_WRONLY);
while((numread = fread(readbuffer, 1, sizeof(readbuffer), file)) > 0){
write(wrfd, readbuffer, numread);
}
pclose(file);
close(wrfd);
}
else{
sscanf(msgbuf.mtext, "%s %s\n", command, filename);
if(strcmp(command, "putfile") == 0){
file = fopen(filename, "w");
rdfd = open(NP1, O_RDONLY);
while((numread = read(rdfd, readbuffer, sizeof(readbuffer))) > 0){
fwrite(readbuffer, 1, numread, file);
}
fclose(file);
close(rdfd);
}
else{
if(strcmp(command, "getfile") == 0){
file = fopen(filename, "r");
wrfd = open(NP2, O_WRONLY);
while((numread = fread(readbuffer, 1, sizeof(readbuffer), file)) > 0){
write(wrfd, readbuffer, numread);
}
fclose(file);
close(wrfd);
}
else{
printf("unknown command\n");
}
}
}
if(msgrcv(msgqid, (struct my_msgbuf*)&msgbuf, sizeof(msgbuf), 0, 0) == -1){
unlink(NP1);
unlink(NP2);
msgctl(msgqid, IPC_RMID, 0);
perror("msgrcv");
exit(1);
}
}
printf("received command : %s\n", msgbuf.mtext);

unlink(NP1);
unlink(NP2);
msgctl(msgqid, IPC_RMID, 0);
printf("server ends\n");

return 0;
}

Pazar, Mayıs 28, 2006

GMail-Chat

Hayat kurtaran(?), kontak listesinizdekilerin Aninda Haberlesme(IM) adreslerine sahip olmadan aninda haberlesmeyi saglayan Gmail-Chat zor anlar geciriyora benziyor. Uptime'im sadece ->
Şu an 1:00ös, 1 gün 17:16, 6 kullanıcı çalışıyor, yük ortalaması: 1,60, 1,45, 1,41
ve bu surede gmail-chat servisi bana 3 kere;
"We're experiencing technical difficulties that may prevent your chats from being sent." dedi!
Peki nasil mi tekrar gmail-chat servisinize kavusuyorsunuz; her bilgisayarci gibi yeniden baslatarak(sign-out/sign-in). :) Insana koskoca Google bile sorun yasiyor dedirtiyor. Umarim insan elinin yaptigi her iste sorun olabilecegini unutanlarin da bu problem karsilarina cikar.

Perşembe, Mayıs 25, 2006

Proje Yonetimi ve Kamusal Musteri

Kamuyla calismanin proje yonetimi acisindan zorluklari;
-Ihale sisteminde sirket referanslarinin degil teklif degerinin goz onune alinmasi
-Kamu da ki bir takim terimlerin hem proje de calisan hem de projeyi kullanacak olan kullanicilar icin ayni anlami ifade etmesi
-Fiyat teklifinden once analiz suresinin olmamasi

Ve bu gun proje gelistirme de guzel bir ders aldim. Calistigim sirketin proje yoneticisi benden bir kagit aldi. Bu kagittan ucan bir cisim yapmanizi istesem ne yapardiniz diye sordu. Toplanti halindeydik ve mirin kirin ederken ucak yapariz dedik hepimiz. Peki hedefi vurabilir misiniz bu ucakla dedi ve burusturdugu kagidi firlatti.

Aldigimiz ikinci ders ise uzmanla muhendis kavramlarinin karistirilmasi uzerineydi. Tekstil muhendisi bir arkadas kuramadigi bir programi bir bilgisayar muhendisinden kurmasini istedigin de onun da tekstil muhendisi arkadasa sokuk coraplarini yamatmak istemesi eglenceli bir ornekti. Arkeologlar icin bir sey bulamadik umarim bu yaziyi okumazlar :).

Crystal Reports - Basic Formula Generations

1) Comparing against a fixed quantity (e.g. 100)
-Choose Report->Select Expert
-Select the column that you are going to compare from the table
-Choose "is less than or equal to" from pull-down list and enter 100 in the comparison field.

2)Comparing against a quantity the varies
-Choose Report->Selection Formulas-> Record
-Write the formula into Formula-Field as;
ex: {Table.Column}+{Table.Column}={Table.Column}

3)Comparing against a quantity that initiated in the runtime.
-In the Field Explorer tree, right-click the Parameter Fileds option and choose "New" command.
-Enter name for the parameter field, some prompting text, and the user is tom make.
-Click the Select Expert
-Select the field you want to compare
-In the pull down list select "is greater than or equal to"
-Pull down the list and Select {?ParameterFieldName}

Pazartesi, Mayıs 22, 2006

Client/Server2 calisti!

Zaman duyarliligi icin sinyal kullanmamiz istenmisti, ilk baslarda senkronize calismasada bir sure sonra senkronize oluyor. Bu arada program calisirken (execution'da), "ctrl+c" ile programi kesersem processlerim ölmüyor, ama terminalden "kill pid" ile öldürebiliyorum! "ctrl+z" ile kesersem ise "kill pid" ise yaramiyor, fakat "kill -9 pid" ile "defunct" olan processler hepsi "defunct" statusune gectikten sonra ölüyorlar.

"A user request to interrupt or terminate the program. Most environments are set up to let a user suspend the program by typing C-z, or terminate it with C-c. Whatever key sequence is used, the operating system sends the proper signal to interrupt the process."

Ctrl+z suspend durumuna geciriyor, anladim da; Ctrl+c programi durdurdugu gibi parent (ne bu ebeveyn mi?) ve cocuk processlerin de ölmesi gerekmiyor mu? Iste buna cevap bulamadim!

Pazar, Mayıs 21, 2006

Client/Server2

Iyisin, hossun da sistemde bir kere calistiktan sonra neden calismiyorsun hic anlamadim!

You are supposed to create 3 periodic tasks (using thread or fork mechanism, it’s up to you).
The period of these tasks are 1, 5 and 10 seconds. The job of the first task is to write “I am task
1” on the screen, for the second task “I am task 2” and so on. Since these tasks are periodic, for
example, the first task will repeatedly say “I am task 1” each second, similarly the other tasks
will periodically run and print their messages on the screen.


#include "stdio.h"
#include"signal.h"

void handler1(int sig)
{
printf("I am task1");
signal(sig, handler1);
}

void handler2(int sig)
{
printf("I am task2");
signal(sig, handler2);
}

void handler3(int sig)
{
printf("I am task3");
signal(sig, handler3);
}

int processFork(void *sighandler){

int pid;

if((pid = fork())== -1){
printf("not forked!");
exit(-1);
}else if(pid == 0){
printf("forked!\n");
signal(SIGUSR1, sighandler);
printf("signalled!\n");
while(1)
{
sigpause(SIGUSR1);
printf("duut\n");
}
exit(0);
}

return pid;

}

int main(void)
{
int seconds;
int process1, process2, process3;

process1 = processFork(handler1);
process2 = processFork(handler2);
process3 = processFork(handler3);

printf("process 1 : %d\n", process1);
printf("process 2 : %d\n", process2);
printf("process 3 : %d\n", process3);
for(seconds = 0; seconds < 100; seconds++){
if(seconds % 1 == 0){
kill(process1, SIGUSR1);
}
if(seconds % 5 == 0){
kill(process2, SIGUSR1);
}
if(seconds % 10 == 0){
kill(process3, SIGUSR1);
}
sleep(1);
}

kill(process1, SIGKILL);
kill(process2, SIGKILL);
kill(process3, SIGKILL);

return 0;
}

Cuma, Mayıs 19, 2006

Client/Server1

Write a benchmark program to make a performance comparison of fork and thread
mechanisms. Your performance comparison will take into account only CPU overhead of
creating threads and child processes. The output of your program should be something like this:
“Creating a thread is %x faster than creating a child process using fork”.

#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "pthread.h"
#include "sys/times.h"

#define THREADCYCLE 500000000
#define FORKCYCLE 500000000

void *print_message_function(void *ptr){
char *message;
message = (char *) ptr;

}

int main(){

struct tms endtime, starttime;
int iterator, return_pid;
pthread_t thread;
int iret;
char *message ="thread";

times(&starttime);
for( ; iterator < THREADCYCLE;){
while( iret == -1){
iret = pthread_create( &thread, NULL, print_message_function, (void*) message);
printf("error");
}
iterator ++;
}
times(&endtime);
printf("bu kadar %d\n",(endtime.tms_utime - starttime.tms_utime));

times(&starttime);
for(iterator =0; iterator < FORKCYCLE;){
while(return_pid == -1){
return_pid = fork();
}
iterator ++;
}
if(return_pid == 0){
exit(0);
}
times(&endtime);
printf("bu kadar %d\n", (endtime.tms_utime - starttime.tms_utime));
}