├── common ├── cleanrecords~ ├── plot └── records │ └── .txt ├── README.md ├── README.md~ ├── common.c ├── server.c └── client.c /common/cleanrecords~: -------------------------------------------------------------------------------- 1 | rm records/* 2 | -------------------------------------------------------------------------------- /common/plot: -------------------------------------------------------------------------------- 1 | plot "common/records/abc.txt" with line 2 | pause -1 "Press enter to continue..." -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | STOCK MARKET SIMULATION 2 | ======================= 3 | 4 | This was my Programming and Data Structures project for the first semester. Its shitty considering that I didn't make full use of the standard C library. Whatever here is within the purview of the syllabus of the course. 5 | 6 | I tried to explore less of stock market simulation algorithms and more of inter-process communication in the project. It used simple file based I/O with locks to do inter-process communication, (a message queue is a better option, and it was out of syllabus). 7 | 8 | Compiling 9 | --------- 10 | To compile the project execute the follwing commands at the Linux Terminal: 11 | 12 | gcc server.c common.c -o server.o 13 | gcc client.c common.c -o client.o 14 | 15 | Running 16 | ------- 17 | First run `server.o` in a separate window followed by `client.o`. The client will not function if the server is not running already. 18 | -------------------------------------------------------------------------------- /README.md~: -------------------------------------------------------------------------------- 1 | STOCK MARKET SIMULATION 2 | ======================= 3 | 4 | This was my Programming and Data Structures project for the first semester. Its shitty considering that I didn't make full use of the standard C library. Whatever here is within the purview of the syllabus of the course. 5 | 6 | I tried to explore less of stock market simulation algorithms and more of inter-process communication in the project. It used simple file based I/O with locks to do inter-process communication, (a message queue is a better option, and it was out of syllabus). 7 | 8 | Compiling 9 | --------- 10 | To compile the project execute the follwing commands at the Linux Terminal: 11 | 12 | gcc server.c common.c -o server.o 13 | gcc client.c common.c -o client.o 14 | 15 | Running 16 | ------- 17 | First run `server.o` in a separate window followed by `client.o`. The client will not function if the server is not running already. 18 | -------------------------------------------------------------------------------- /common.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define SERVER_STATUS "common/serverStatus.dat" // file where the status of the server is stored 3 | 4 | FILE *f; 5 | 6 | struct serverStatus{ 7 | int isRunning; 8 | char signal; 9 | };// this structure stores the running status and used to communicate signals of the server 10 | 11 | void setRunning(int state){//this updates the status of the server to specify its running or not 12 | struct serverStatus status; 13 | f=fopen(SERVER_STATUS,"w+"); 14 | fread(&status,sizeof(struct serverStatus),1,f); 15 | status.isRunning=state; 16 | fwrite(&status,sizeof(struct serverStatus),1,f); 17 | fclose(f); 18 | } 19 | 20 | int isRunning(){// this returns whether the server is currently running or not 21 | struct serverStatus status; 22 | f=fopen(SERVER_STATUS,"r"); 23 | fread(&status,sizeof(struct serverStatus),1,f); 24 | fclose(f); 25 | return status.isRunning; 26 | } 27 | 28 | void setSignal(char sig){//this sets a signal on the server that will used for client-server communication 29 | struct serverStatus status; 30 | f=fopen(SERVER_STATUS,"w+"); 31 | fread(&status,sizeof(struct serverStatus),1,f); 32 | status.signal=sig; 33 | fwrite(&status,sizeof(struct serverStatus),1,f); 34 | fclose(f); 35 | } 36 | 37 | char getSignal(char sig){//this gets the current signal that has been currently set on the server 38 | struct serverStatus status; 39 | f=fopen(SERVER_STATUS,"r"); 40 | fread(&status,sizeof(struct serverStatus),1,f); 41 | fclose(f); 42 | return status.signal; 43 | } 44 | -------------------------------------------------------------------------------- /server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #define MAX_STOCKS 100 6 | 7 | struct stock{ 8 | char name[51]; 9 | int baseVal; 10 | int currVal; 11 | int incr; 12 | }; 13 | 14 | struct stock stocks[MAX_STOCKS]; 15 | 16 | extern void setRunning(int); 17 | extern void setSignal(char); 18 | extern int isRunning(); 19 | extern char getSignal(); 20 | void loop(); 21 | void readStocks(); 22 | void willIncr(); 23 | void writeDefault(); 24 | void processStocks(); 25 | void updateRecords(); 26 | void printStocks(); 27 | void commit(); 28 | 29 | int turn=0; 30 | 31 | int main(){ 32 | setRunning(1); 33 | system("rm common/records/*"); 34 | printf("Stocks server started successfuly!!\n"); 35 | loop(); 36 | printf("Stocks server exiting!\n"); 37 | return 0; 38 | } 39 | 40 | void loop(){ 41 | while(1){ 42 | if(getSignal()=='w') 43 | printf("Waiting for another process to finish access!\n"); 44 | else if(getSignal()=='q') 45 | break; 46 | else{ 47 | turn++; 48 | readStocks(); 49 | willIncr(); 50 | processStocks(); 51 | printStocks(); 52 | updateRecords(); 53 | commit(); 54 | printf("Stock Values updated!!\n"); 55 | } 56 | sleep(3); 57 | } 58 | } 59 | 60 | void printStocks(){ 61 | int i; 62 | for(i=0;i(rand()%75)) 102 | stocks[i].incr=0; 103 | else 104 | stocks[i].incr=1; 105 | } 106 | } 107 | 108 | void processStocks(){ 109 | int i; 110 | for(i=0; i 2 | #include 3 | #include 4 | #include 5 | 6 | #define MAX_STOCKS 100 // maximum number of stocks that are possible 7 | 8 | int c,q=0; 9 | char loginpath[80]; 10 | 11 | struct stock{ 12 | char name[51]; 13 | int baseVal; 14 | int currVal; 15 | int incr; 16 | };// structure stores the current details of the stocks available in the market 17 | 18 | struct user{ 19 | char name[51]; 20 | char password[9]; 21 | int num[20]; 22 | int basePrice[20]; 23 | char stocks[20][51]; 24 | long profit; 25 | long capital; 26 | };// this stores the details of the users - name, password, stocks bought, etc. 27 | 28 | struct user u; 29 | struct stock stocks[MAX_STOCKS]; 30 | 31 | extern void setRunning(int); 32 | extern void setSignal(char); 33 | extern int isRunning(); 34 | extern char getSignal(); 35 | void quit(); 36 | void company(); 37 | void customer(); 38 | void getQuote(); 39 | void getGraph(); 40 | void createStock(); 41 | void registerUser(); 42 | void availablestocks(); 43 | void buyStocks(); 44 | void login(); 45 | void loggedin(); 46 | void logout(); 47 | void sellStocks(); 48 | void myPortfolio(); 49 | 50 | int main(){ 51 | if(!isRunning())//checking whether the server is running or not 52 | printf("Stocks server is not running! Please run the stocks server before running the client!\n"); 53 | else{ 54 | do{ 55 | printf("\n\tVirtual Stock Market Management System\n\t---------------------------------------\n\n"); 56 | printf("1. Company Login - Create stocks and view current prices/analysis\n"); 57 | printf("2. Customer Login - Buy and Sell Stocks\n"); 58 | printf("3. Exit\n\n"); 59 | printf("Enter choice:"); 60 | scanf("%d",&c); 61 | switch(c){ 62 | case 1: 63 | company(); 64 | break; 65 | case 2: 66 | customer(); 67 | break; 68 | case 3: 69 | quit(); 70 | break; 71 | default: 72 | printf("Invalid choice! Enter again.\n\n"); 73 | } 74 | }while(!q); 75 | } 76 | return 0; 77 | } 78 | 79 | void quit(){//code to quit the client - this asks to option to quit the server as well 80 | char chr; 81 | printf("Stop the server as well? (y/n) "); 82 | getchar(); 83 | chr=getchar(); 84 | if(chr=='y'){ 85 | setRunning(0); 86 | setSignal('q');// passing the quit signal to the server 87 | } 88 | q=1; 89 | } 90 | 91 | void company(){// function to print the company login menu 92 | int p=1; 93 | do{ 94 | printf("\nWelcome to the company management console!\n\n"); 95 | printf("1. View current stock price of a company\n"); 96 | printf("2. View past stock history of the company\n"); 97 | printf("3. Create a new stock for a company\n"); 98 | printf("4. Go Back\n\n"); 99 | printf("Enter choice:"); 100 | scanf("%d",&c); 101 | switch(c){ 102 | case 1: 103 | getQuote(); 104 | break; 105 | case 2: 106 | getGraph(); 107 | break; 108 | case 3: 109 | createStock(); 110 | break; 111 | case 4: 112 | p=0; 113 | break; 114 | default: 115 | printf("Invalid Choice. Enter again!\n"); 116 | } 117 | }while(p); 118 | } 119 | 120 | void getQuote(){//function fetches the current value of stock in the market 121 | char cmp[51]; 122 | printf("Enter the name of the company whose stock you want to get: "); 123 | scanf("%s",cmp); 124 | setSignal('w');// setting the wait signal on server to prevent file-locking conflicts 125 | int i,found=0; 126 | FILE *f=fopen("common/stocks.dat","r+"); 127 | fread(stocks,sizeof(struct stock),MAX_STOCKS,f); 128 | for(i=0;iu.num[j]) 428 | { 429 | printf("\nYou cannot sell more stocks than you possess\n"); 430 | break; 431 | } 432 | else { 433 | u.capital+=price; 434 | u.num[j]-=qty; 435 | if (u.num[j]==0) 436 | strcpy(u.stocks[j],""); 437 | printf("\nStocks sold successfully!!\n You have earned Rs. %ld \n Your current capital is Rs.%ld\n\n",price,u.capital); 438 | break; 439 | } 440 | }else 441 | printf("\nYou have not bought any stocks of this company!\n"); 442 | } 443 | printf("Press enter to continue...\n"); 444 | getchar(); 445 | getchar(); 446 | } 447 | 448 | void myPortfolio(){ 449 | int i,j; 450 | printf("\n\tMY PORTFOLIO\n\n"); 451 | printf("Name: %s\n",u.name); 452 | printf("Capital: %ld\n\n",u.capital); 453 | printf("------------------------------------------------------------\n"); 454 | printf("Name | Quantity | Unit market price | Total price\n"); 455 | printf("------------------------------------------------------------\n"); 456 | for(j=0;j<20;j++){ 457 | FILE *f=fopen("common/stocks.dat","r+"); 458 | fread(stocks,sizeof(struct stock),MAX_STOCKS,f); 459 | fclose(f); 460 | for(i=0;i